Take the 2-minute tour ×

I know Ruby very well. I believe that I may need to learn Python presently. For those who know both, what concepts are similar between the two, and what are different?

I'm looking for a list similar to a primer I wrote for Learning Lua for JavaScripters: simple things like whitespace significance and looping constructs; the name of nil in Python, and what values are considered "truthy"; is it idiomatic to use the equivalent of map and each, or are mumble _somethingaboutlistcomprehensions_ mumble the norm?

If I get a good variety of answers I'm happy to aggregate them into a community wiki. Or else you all can fight and crib from each other to try to create the one true comprehensive list.

Edit: To be clear, my goal is "proper" and idiomatic Python. If there is a Python equivalent of inject, but nobody uses it because there is a better/different way to achieve the common functionality of iterating a list and accumulating a result along the way, I want to know how you do things. Perhaps I'll update this question with a list of common goals, how you achieve them in Ruby, and ask what the equivalent is in Python.

share|improve this question
the only thing i read was c2.com/cgi/wiki?PythonVsRuby , i really dont like self and the indentions but i got used to it :) –  Saif al Harthi Jan 22 '11 at 16:28
Related: stackoverflow.com/questions/1113611/… (I'm not quite sure if it's a duplicate, as that question asks for things without an equivalent). –  delnan Jan 22 '11 at 16:29
@Saif That discussion had a lot of outdated and bad examples last time I checked. Suggesting a=[];0.upto(2){|i|0.upto(2){|j|a<<[i,j] if i!=j}} as a Ruby equivalent of [(x,y) for x in xrange(3) for y in xrange(3) if x != y] (and then removing all whitespaces and calling that an improvement) is pretty much what you risk doing when you don't heed ircmaxell's advice. –  badp Jan 22 '11 at 16:37
@SilentGhost I strongly disagree. I am asking "What is the same between the languages, and what is different?" As shown by many of the answers below, there are very clear and helpful answers possible for this. –  Phrogz Jan 22 '11 at 17:32
@Phrogz: I see that and makes the question un-answerable. –  SilentGhost Jan 22 '11 at 18:02
show 7 more comments

5 Answers

up vote 92 down vote accepted

Here are some key differences to me:

  1. Ruby has blocks; Python does not.

  2. Python has functions; Ruby does not. In Python, you can take any function or method and pass it to another function. In Ruby, everything is a method, and methods can't be directly passed. Instead, you have to wrap them in Proc's to pass them.

  3. Ruby and Python both support closures, but in different ways. In Python, you can define a function inside another function. The inner function has read access to variables from the outer function, but not write access. In Ruby, you define closures using blocks. The closures have full read and write access to variables from the outer scope.

  4. Python has list comprehensions, which are pretty expressive. For example, if you have a list of numbers, you can write

    [x*x for x in values if x > 15]

    to get a new list of the squares of all values greater than 15. In Ruby, you'd have to write the following:

    values.select {|v| v > 15}.map {|v| v * v}

    The Ruby code doesn't feel as compact. It's also not as efficient since it first converts the values array into a shorter intermediate array containing the values greater than 15. Then, it takes the intermediate array and generates a final array containing the squares of the intermediates. The intermediate array is then thrown out. So, Ruby ends up with 3 arrays in memory during the computation; Python only needs the input list and the resulting list.

    Python also supplies similar map comprehensions.

  5. Python supports tuples; Ruby doesn't. In Ruby, you have to use arrays to simulate tuples.

  6. Ruby supports switch/case statements; Python does not.

  7. Ruby supports the standard expr ? val1 : val2 ternary operator; Python does not.

  8. Ruby supports only single inheritance. If you need to mimic multiple inheritance, you can define modules and use mix-ins to pull the module methods into classes. Python supports multiple inheritance rather than module mix-ins.

  9. Python supports only single-line lambda functions. Ruby blocks, which are kind of/sort of lambda functions, can be arbitrarily big. Because of this, Ruby code is typically written in a more functional style than Python code. For example, to loop over a list in Ruby, you typically do

    collection.each do |value|

    The block works very much like a function being passed to collection.each. If you were to do the same thing in Python, you'd have to define a named inner function and then pass that to the collection each method (if list supported this method):

    def some_operation(value):

    That doesn't flow very nicely. So, typically the following non-functional approach would be used in Python:

    for value in collection:
  10. Using resources in a safe way is quite different between the two languages. Here, the problem is that you want to allocate some resource (open a file, obtain a database cursor, etc), perform some arbitrary operation on it, and then close it in a safe manner even if an exception occurs.

    In Ruby, because blocks are so easy to use (see #9), you would typically code this pattern as a method that takes a block for the arbitrary operation to perform on the resource.

    In Python, passing in a function for the arbitrary action is a little clunkier since you have to write a named, inner function (see #9). Instead, Python uses a with statement for safe resource handling. See How do I correctly clean up a Python object? for more details.

share|improve this answer
3. Python 3 nonlocal fixes this 4. Python also gives you generator expressions (similar to list comprehensions, but don't compute anything until asked to - think of list comprehensions as generator expressions fed to list (which takes an iterable and returns a list containing everything the iterable yielded) - this can save much effort in some cases). –  delnan Jan 22 '11 at 16:48
7. Yes it does. val1 if expr else val2. 8. Although I see it mostly used for mixin-style augmentation. –  delnan Jan 22 '11 at 17:00
@Phrogz For small cases, if/elsif is the way to go. For bigger cases, building a map that maps the case to a function for that case is probably better. This is one of the contentious areas in Python, and it's one where Guido has kind of dug in his feet. –  Clint Miller Jan 22 '11 at 17:32
Your ruby example in #4 is not idiomatic. It would be more ruby-ish (and readable) to write values.map{|v| v*v if v > 15}.compact. IMHO, this is even more expressive (and certainly clearer) than your python example. –  sml Jan 27 '11 at 16:59
In addition to the above, using the ! version of the compact function avoids a copy of the array: values.map{|v| v*v if v > 15}.compact!. This means that only the input list and resulting list exist in memory. See #4 here: igvita.com/2008/07/08/6-optimization-tips-for-ruby-mri –  sml Jan 27 '11 at 17:11
show 10 more comments

I've just spent a couple of months learning Python after 6 years of Ruby. There really was no great comparison out there for the two languages, so I decided to man up and write one myself. Now, it is mainly concerned with functional programming, but since you mention Ruby's inject method, I'm guessing we're on the same wavelength.

I hope this helps: The 'ugliness' of Python

A couple of points that will get you moving in the right direction:

  • All the functional programming goodness you use in Ruby is in Python, and it's even easier. For example, you can map over functions exactly as you'd expect:

    def f(x):
        return x + 1
    map(f, [1, 2, 3]) # => [2, 3, 4]
  • Python doesn't have a method that acts like each. Since you only use each for side effects, the equivalent in Python is the for loop:

    for n in [1, 2, 3]:
        print n
  • List comprehensions are great when a) you have to deal with functions and object collections together and b) when you need to iterate using multiple indexes. For example, to find all the palindromes in a string (assuming you have a function p() that returns true for palindromes), all you need is a single list comprehension:

    s = 'string-with-palindromes-like-abbalabba'
    l = len(s)
    [s[x:y] for x in range(l) for y in range(x,l+1) if p(s[x:y])]
share|improve this answer
Sigh, I read that post and it confirms my suspicion: few people understand the role and utility of special methods in Python. They're incredibly useful and standardized, and they're underscored like so to avoid naming conflicts with the builtins that they often implement. No one who actually knows Python is trying to discourage their use. –  Rafe Kettler Jan 22 '11 at 23:05
You don't seem to understand how methods work. A method is, essentially, a function whose first argument is an instance of the class the method belongs to. When you write Class.method, the method is "unbound" and the first argument should be a Class instance; when you write object.method, the method is "bound" to the object instance of Class. This lets you choose whether to use map (etc) to call the method on a difference instance each time (pass an unbound method), or to keep the instance fixed and pass a different second argument each time. Both are useful. –  LaC Jan 23 '11 at 21:38
You're right, I didn't understand how they worked. Since publishing the article, I've gotten a better sense for it. Thanks! –  David J. Jan 25 '11 at 2:12
[s[x:y] for x in range(l) for y in range(x,l+1) if p(s[x:y])] - this line shows how Python hard for reading is. When you read Ruby code you move your eyes from left to right, without returnings. But to read Python code, you need to go left-right-left-right-left-right... and parentheses, parentheses, parentheses, parentheses... Also in Python you often need mixing of methods and function. It's madness: E(C(A.B()).D()) instead of Ruby's A.B.C.D.E –  Nakilon Jan 30 '11 at 23:13
@Nakilon That's why you should only use nested list comprehensions for really simple cases, and not like the above. It might be 'clever' to write a one-liner that finds all palindromes in a string, but it's best reserved for code golf. For real code that someone else has to read later, you would just write a few line function. So yes, that line is hard to read, but that's the programmer's fault, not the language's. –  Cam Jackson Oct 24 '13 at 2:44
add comment

My suggestion: Don't try to learn the differences. Learn how to approach the problem in Python. Just like there's a Ruby approach to each problem (that works very well givin the limitations and strengths of the language), there's a Python approach to the problem. they are both different. To get the best out of each language, you really should learn the language itself, and not just the "translation" from one to the other.

Now, with that said, the difference will help you adapt faster and make 1 off modifications to a Python program. And that's fine for a start to get writing. But try to learn from other projects the why behind the architecture and design decisions rather than the how behind the semantics of the language...

share|improve this answer
I appreciate your suggestion. I wholly agree with the sentiment (which I interpret as "Learn to program idiomatic Python"). That's precisely what I'm trying to do. I'm not asking "What is the Python name for Ruby's each method?" I'm asking "How are things done properly in Python different from Ruby, and where are they done properly the same?" If Python's false is actually False, that's as important to know as where and when I should do things in a Rubyesque way, and where and when I should not. –  Phrogz Jan 22 '11 at 16:38
@Phrogz: That's fair. The way I interpreted your question was: Let's make a list of the differences so we can just change the language we're programming in. But it's a fair question. I guess I just mis-interpreted what you were asking for. I'll leave this here for reference, but it'll be interesting to see what else comes up... –  ircmaxell Jan 22 '11 at 16:44
add comment

I know little Ruby, but here are a few bullet points about the things you mentioned:

  • nil, the value indicating lack of a value, would be None (note that you check for it like x is None or x is not None, not with == - or by coercion to boolean, see next point).
  • None, zero-esque numbers (0, 0.0, 0j (complex number)) and empty collections ([], {}, set(), the empty string "", etc.) are considered falsy, everything else is considered truthy.
  • For side effects, (for-)loop explicitly. For generating a new bunch of stuff without side-effects, use list comprehensions (or their relatives - generator expressions for lazy one-time iterators, dict/set comprehensions for the said collections).

Concerning looping: You have for, which operates on an iterable(! no counting), and while, which does what you would expect. The fromer is far more powerful, thanks to the extensive support for iterators. Not only nearly everything that can be an iterator instead of a list is an iterator (at least in Python 3 - in Python 2, you have both and the default is a list, sadly). The are numerous tools for working with iterators - zip iterates any number of iterables in parallel, enumerate gives you (index, item) (on any iterable, not just on lists), even slicing abritary (possibly large or infinite) iterables! I found that these make many many looping tasks much simpler. Needless to say, they integrate just fine with list comprehensions, generator expressions, etc.

share|improve this answer
Generator expressions are cool. They give Python a bit of the lazy evaluation capabilities of languages like Haskell. –  Clint Miller Jan 22 '11 at 17:40
@Clint: Yes. And full generators are even more capable (although not needed for simple cases, which happen to be the majority). –  delnan Jan 22 '11 at 17:42
Why do you check with x is None or x is not None? I always check with x == None and x != None. –  John Jan 22 '11 at 18:00
@John: If x defines __eq__ in a silly way, it could give a false positive. If the __eq__ isn't programmed carefully enough, it could crash (e.g. AttributeError) when given certain values (i.e. None). On the contrary, is can't be overridden - it always compares object identity, which is the right (most robust, simplest and cleanest) way to check for a singleton. –  delnan Jan 22 '11 at 18:04
(Not trying to be argumentative, just learning) How would x overload __eq__? Would this only happen when __eq__ was not overwritten? –  John Jan 22 '11 at 18:09
show 4 more comments

In Ruby, instance variables and methods are completely unrelated, except when you explicitly relate them with attr_accessor or something like that.

In Python, methods are just a special class of attribute: one that is executable.

So for example:

>>> class foo:
...     x = 5
...     def y(): pass
>>> f = foo()
>>> type(f.x)
<type 'int'>
>>> type(f.y)
<type 'instancemethod'>

That difference has a lot of implications, like for example that referring to f.x refers to the method object, rather than calling it. Also, as you can see, f.x is public by default, whereas in Ruby, instance variables are private by default.

share|improve this answer
add comment

Your Answer


