Contrasting Python and Ruby
{Thursday, May 3rd, 2007} Admittedly I’ve been caught in the community currents moving towards Ruby and Rails development triggered by the Pragmatic Programmers. While on the ride I’ve been weary of their seemingly direct competitors, Python and Django.
The choice of which to use has usually been an emotion charged debate.
While others have poked around and performing comparisons, I thought I’d take a quick look for myself.
There are two comparisons at play here which I’ll split into two entries: Python vs. Ruby, Django vs. Rails.
Here’s my take on Python vs. Ruby. Granted it’s a long way off comprehensive, biased by my knowledge of Ruby.
Ruby
Pro’s
- Everything is an object
- Method discovery built into the language
- Language reads more naturally, take optional parenthesis as an example
- ! and ? methods suffixes are a nice standard for communicating intent
- return optional
- More object focused
- Often more readable
- IntelliJ IDE support
Con’s
- Slower
- Less Mature
Python
Pro’s
- Blocks via indentation more terse
- Class methods can be easily treated as function objects
- No need for @, @@, $ as namespaces are used
- Method arguments can be passed by name
- Automatically generates compiled scripts for efficiency
Con’s
- pass required for empty blocks
- Parenthesis required on method calls
- self required as first argument to methods
- return required
- Verbose import statements, e.g. from package.subpackage.module import class, class.method.
- Why import an individual method or variable?!
- Is from datetime import datetime DRY?!
- Less object focused, attributes are also first class citizens
- Method documentation (docstring) within method doesn’t aid readability
- Method default values are only evaluated once and reused in subsequent calls
- Non-OO approach to sequence manipulation and list comprehensions with methods such as map, del, sort and sorted
- Dictionaries throw an exception when key not found
- No concept of scope, class internals completely exposed. Use convention (such as underscore prefix) to suggest scope
- Ruby has Rake and Python has ?!
Weird stuff
- zip is an unusual name for iterating over multiple sequences
- Comparison chaining is unorthodox, i.e. 1 < 2 == 2
- ‘Isn’t’ produces the string “Isn’t”. However ‘Isn’t”‘ produces the string ‘Isn’t”‘. In the latter case a backslash remains and the start and end quotes are different.
Similarities
- The languages feel remarkably similar. They both seem as terse.
- Gem’s and Egg’s
- JRuby and Jython
- IronRuby and IronPython
Other discussions of Ruby vs. Python:
