Broke my heart when he said you shouldn't need powerful anonymous functions :(
Python for Ruby Programmers (LA RubyConf 2013)
21–30 of 54 posts
Re: Python for Ruby Programmers (LA RubyConf 2013)
#22Earlier 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.
Re: Python for Ruby Programmers (LA RubyConf 2013)
#23* 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)
#24Earlier 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)
Re: Python for Ruby Programmers (LA RubyConf 2013)
#25To 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…
+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)
#26Author 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! :)
Re: Python for Ruby Programmers (LA RubyConf 2013)
#27Thanks 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.
Re: Python for Ruby Programmers (LA RubyConf 2013)
#28Technically, 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)
Re: Python for Ruby Programmers (LA RubyConf 2013)
#29Thanks 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.
Re: Python for Ruby Programmers (LA RubyConf 2013)
#30One 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.
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.