Live data from Hacker News

Python for Ruby Programmers (LA RubyConf 2013)

speakerdeck.com

21–30 of 54 posts

Re: Python for Ruby Programmers (LA RubyConf 2013)

#21
post #3

Broke my heart when he said you shouldn't need powerful anonymous functions :(

It's so highly unlikely you'll get them, you should probably resign yourself (or use a different language, or try to approach it from the other direction: convert Python statements to expressions so you can use them in existing lambdas, after all Python's current lambdas are no more restricted than Haskell's, it's just that the rest of Python doesn't play nice with them)

Re: Python for Ruby Programmers (LA RubyConf 2013)

#22
post #17

Earlier quoted context omitted.

Interestingly you can do this: class Foo: def bar(self): return "bar" foo = Foo() foo.bar() # returns "bar" Foo.bar(foo) # returns "bar" So it's not actually specifying `self` for instance methods; it's just a special thing about class instances that calling a method will call its class's method passing the instance as the first argument. After realizing this, I was enlightened.

> Foo(foo, bar) # returns "bar" I think you meant `Foo.bar(foo)` here.

You're right. I'm not thinking.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#23
To add to other comments, things the author could have expanded upon:

* vararg keyword arguments (kwargs)

* Keyword-only arguments (Python 3)

* help, vars, a few other nice introspective builtins

* Assignment unpacking (multiple names as left-hand side of an assignment)

* The strangest "first blush" difference between Ruby and Python, or at least the one which I found oddest when I first started switching, is that a new Ruby class is defined from the `class {Name}` line, the whole body executes in the context of the already-created class. Not so in Python, the class object will only exist once the body has finished executing.

* No mention of generators? Also outer v inner iteration is a pretty big split between two otherwise similar languages.

Mistake-ish

* obj.__dict__ -> vars(obj)

* obj.__class__ -> type(obj)

* Python's lambdas are not one-line, they're one-expression. The reason you "can't do [this] in Python" is that if:elif:else: is a statement, so doesn't fit in lambdas. But you can have arbitrary complex code in lambdas aside from that, by following http://codon.com/programming-with-nothing. For instance you can (although you should not) write:

    map(lambda num:
            0 if num % 3 == 0 else
            num * 2 if num % 4 == 0 else
            num,
        range(1, 6))
* Author talks about methods a lot. They're not methods, a method is part of an object. They're functions. Granular functions, inner functions, ...

* We generally don't say Python values "evaluate to False", we say they're falsy: they're never equal to False (outside of False and — for historical reasons — 0), they're just equivalent when evaluated in a boolean context (or passed to `bool()`). Also, the list is way incomplete: any Python type defines its own "falsiness" by implementing __bool__ (or __nonzero__ in Python 2)

* Python's reflection and metaprogramming are roughly on par with Ruby's, their lack of use is more of a cultural artefact (possibly historically motivated) than a technical one. Consider adding methods to existing types for instance. You can do it in Python (on python-defined types), you just very rarely do, it's not in the culture to do something like that. Same with using metaclasses, it's done but it generally isn't the first thing a Python developer will reach for, aside from things like registering classes on the fly/on definition

Odd comments:

* Ruby mixins, as far as I know, work by injecting the module in the inheritance chain somewhere above the new class. MI is clearer and just as powerful.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#24
post #12

Earlier quoted context omitted.

Using list comprehensions as a replacement for filter and map I can see. I don't really see where you're getting at as far as substitutes for 'reduce' are concerned. For me, the 'operator' module is a justification for using 'reduce', not a replacement for it.

> I don't really see where you're getting at as far as substitutes for 'reduce' are concerned. Reduce simply isn't used much in Python code, as a product of both culture and rather lacking lambdas. It's used so little (pretty much the only "usual use case" is covered by `sum`) it's been moved out of the builtins and to the functools module in Python 3 (whereas `map` and `filter` have remained)

Thank you, that was much more eloquently put than I could have provided. Indeed, `sum` was the one common use case I could think of, so I didn't bother including it as an example. I honestly can't say I miss `reduce` at all when writing python.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#25

To add to other comments, things the author could have expanded upon: * vararg keyword arguments ( kwargs) * Keyword-only arguments (Python 3) * help, vars, a few other nice introspective builtins * Assignment unpacking (multiple names as left-hand side of an assignment) * The strangest "first blush" difference between Ruby and Python, or at least the one which I found oddest when I first started switching, is that a…

> Python's reflection and metaprogramming are roughly on par with Ruby's, their lack of use is more of a cultural artefact than a technical one.

+1 on everything you listed, but especially this. You _do_ see some examples of monkey-patching in Python, but at least in my very limited experience, it tends to be much less prevalent in Python than it does in Ruby. Any data to contradict this is welcome, however.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#26

Author here: Thanks much for the feedback. I definitely have more experience in Ruby than Python. In a longer version of this talk, I go into more detail on list comprehensions, iterators and decorators. I'll work to include these to some degree for the shorter talk too. Also, if I say "Python doesn't have these," I'm often saying it in support of Python! :)

I definitely appreciated the open-mindedness of the presentation, and I thank you for a comparison without any petty language-flaming :)

Re: Python for Ruby Programmers (LA RubyConf 2013)

#27
post #11
post #2

Thanks for posting. As a Ruby programmer I have been wanting to learn Python since 2 weeks and hopefully this will get me started for good.

Except if you have a good reason for wanting to learn Python, I will say as a rubyist you should learn a functional programming language like clojure or scala if you don't know them already. This is because both Python and Ruby are OO and similar in alot of ways.

Good point! I know C# and I'm learning Ruby. Next I'll pick clojure for functional programming. We should learn a new language that will change the way of our thinking.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#28

Technically, you can open up Python classes: class Test(object): pass t = Test() def test(self): print('ehlo') Test.test = test t.test() This is rarely done in practice, however (at least, as far as I can tell)

It's awesome, and that's how monkey-patching is done.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#29
post #11
post #2

Thanks for posting. As a Ruby programmer I have been wanting to learn Python since 2 weeks and hopefully this will get me started for good.

Except if you have a good reason for wanting to learn Python, I will say as a rubyist you should learn a functional programming language like clojure or scala if you don't know them already. This is because both Python and Ruby are OO and similar in alot of ways.

I would say you are right except in one case, which is scientific computing. The OP touched on it, but that is definitely the "killer app" of the Python ecosystem, IMHO. Not that the languages you mentioned don't have equivalents, but if you are a rubyist who just wants to do some heavy-duty scientific/data-intensive computing without stepping too far out of your comfort zone, I can see Python being an attractive option.

Re: Python for Ruby Programmers (LA RubyConf 2013)

#30
post #10

One of the things that bothers me about python is the way you have to specify 'self' for instance methods. Instance methods should be the norm not the exception.

No, it's awesome.

It makes "morphing code", such as decorators easier to write. It's explicit. It's consistent (with class methods for instance). And it's not some special case magic with special syntax. It's just another parameter.

Python's great strengths is avoiding special case magic. Much more than many people realize as there's lots of syntactic sugar on top of double underscore functions and interfaces like context manager and generators.

Post reply on HN