LWN.net Logo

Python/Ruby

Python/Ruby

Posted Feb 9, 2008 17:52 UTC (Sat) by rfunk (subscriber, #4054)
In reply to: Interview: Mark "Markey" Kretschmann (Not the Gentoo Weekly News) by tjc
Parent article: Interview: Mark "Markey" Kretschmann (Not the Gentoo Weekly News)

Well, I'm probably more "religiously affiliated" than you'd like, but I 
try to be fairly objective about these things....

I was a Perl and C programmer for a long time.  I've played around with 
Python a bit, and kept getting annoyed by its object model.  It's 
inconsistent, and required using class methods where I expected to be 
able to use object methods.  Then there's the seemingly pointless 
dichotomy between tuples and arrays; and the limit of one expression 
inside a lambda also seems pointless.

I also don't like Python's inability to be programmed functionally; 
side-effects abound, and it forces separating every little action into a 
temporary variable and another line.

I don't care about the syntax.  Much.  Significant indentation is fine, 
though in Python I do tend to miss having some sort of definite markers 
to show where blocks end.

Then I discovered Ruby, and finally found a language with a complete 
object model -- *everything* is an object.  Ruby is the first language I 
used where the OO was complete enough to be both sensible and useful.  
Methods can be quickly and easily chained together (though watch out for 
potentially nil objects).  Functional programming is easy, though not 
required.

And I've only scratched the surface of what Ruby is capable of; its 
meta-programming capabilities give quite a bit of power.

Basically Ruby has Lisp's power, Smalltalk's OO, Perl's 
programmer-friendliness (or better), but it fixes most of the problems of 
all of those.

Python has more popularity now because its popularity started earlier 
(and because Perl stagnated), and there are definitely major surface 
similarities between Python and Ruby (dynamic, OO), but Ruby is capable 
of so much more.

As far as I'm concerned, Ruby's biggest problems are its slowness and its 
library immaturity.  Improving the speed is a major goal of the 
developers now, and the next version should have much better speed; in 
addition there are other implementations with better speed, including 
Rubinius and JRuby.   The libraries are quickly catching up to Python, 
and if you use JRuby you get the benefit of Java libraries.


(Log in to post comments)

Python/Ruby

Posted Feb 9, 2008 21:02 UTC (Sat) by tjc (subscriber, #137) [Link]

> the limit of one expression inside a lambda also seems pointless.

> I also don't like Python's inability to be programmed functionally; side-effects abound, and
it forces separating every little action into a temporary variable and another line.

Are either of these issues addressed by Python 3.0?  I've googled around a bit, but not found
anything conclusive.

Python/Ruby

Posted Feb 10, 2008 0:41 UTC (Sun) by jmtapio (subscriber, #23124) [Link]

I also don't like Python's inability to be programmed functionally; side-effects abound, and it forces separating every little action into a temporary variable and another line.

Care to elaborate on that? What is missing from Python that _prevents_ functional programming?

And Python does not _force_ opening up one-liners, but it does promote clear code. Just google up "obfuscated python" and there will be a plethora of code examples where "every little action" is not separated.

Python/Ruby

Posted Feb 10, 2008 16:09 UTC (Sun) by rfunk (subscriber, #4054) [Link]

Um, you quoted some, and I mentioned another you didn't quote.

1. Lambdas are limited to a single expression.
2. Too many operations have side effects -- they change their argument rather than 
returning the changed version leaving their argument unchanged.
3. This in turn requires lots of separating of operations and defining temporary variables 
if you don't in fact want the original thing changed.

There's actually a web page I found on programming Python functionally, and it mentions 
Python's abilities and limitations in that area (as well as giving some workarounds).
  http://scott.andstuff.org/FunctionalPython
(For example, it mentions Python's lack of a ternary operator; tricks with and/or are 
definitely obfuscation.)
Another one that gets more to my points, but from a Pythonists's perspective:
  http://withoutane.com/rants/2007/01/embrace-the-heresy
"But if you try to write your whole program in a functional style, you always seem to be 
going against the grain of the language."

Take a look at the operators (well, methods) in the standard Ruby classes, and you'll see 
that support for clear, concise functional programming abounds, with no need for 
workarounds or obfuscated-looking code.  Ruby combines OO and functional 
programming nicely and elegantly, without forcing functional on you.

I do notice, however, that Python has added more support for lists than it had the last 
time I delved deeply into it.

Python/Ruby

Posted Feb 10, 2008 21:30 UTC (Sun) by jmtapio (subscriber, #23124) [Link]

With regards to lambda I disagree. I find there are very few cases where even using a lambda at all is an advantage over giving the function a name. And if you are going to be using statements within the function, why not go all the way and define that function with a statement? After the introduction of list comprehensions several years ago I personally stopped using lambdas, and I do prefer a somewhat functional style myself.

If side effects bother you, don't use functions that operate in-place. For example instead of mylist.sort() do sorted(mylist). Note that you do not have to do something like templist = mylist[:]; templist.sort() as would have been the case a few years ago. I think it is a good thing for a language to have both types of operations available because sometimes there are good performance justifications for using in-place-algorithms.

The first example URL that you gave seems to point to a quite old page. The examples there would have been valid for some really old Python versions. The first example could nowadays be written like this:

[x for x in [1,2,5,6,8,12,15,17,20,23,24] if x % 2 == 0]

And calculating the sum of a sequence can be done by callind sum(sequence) without defining any functions or without any temporary variables. I would like to again reiterate that Python does not _prevent_ using functional style and it does give quite a lot of syntax and tools to make it easier, but fundamentally there are always the procedural and object oriented parts of the language available as well. And the generic mentality among pythonists is to avoid dense hard-to-read code, even if it were mathematically beautiful. I used to write much more functional code in the past until I got more interested making code easy to read for non-wizard people.

(For example, it mentions Python's lack of a ternary operator; tricks with and/or are definitely obfuscation.)

Python does have a ternary operator.

>>> 'foo' if True else 'bar'
'foo'
>>> 'foo' if False else 'bar'
'bar'

Ruby is often mentioned as better than Python based on something that Python did not support in the past. For some reason language knowledge travels very poorly between the two camps and that has in the past years caused a lot of unnecessary rumors and unfounded claims of inferiority/superiority. (I am not accusing you per se, I'm just trying to note that a lot of people over the years have brought up the same points you did, and most of them have been "fixed" years ago in Python.)

Python/Ruby

Posted Feb 11, 2008 1:23 UTC (Mon) by ikm (subscriber, #493) [Link]

> Python does have a ternary operator.
> >>> 'foo' if True else 'bar'
>'foo'
> >>> 'foo' if False else 'bar'
> 'bar'

Which version of Python is that? The one I have right now as a system default (2.4.4, Debian
sid) barfs at the given syntax.

Python/Ruby

Posted Feb 11, 2008 6:52 UTC (Mon) by jmtapio (subscriber, #23124) [Link]

That syntax was added in 2.5.0 (released in September 2006). It is in Debian stable (python2.5) but the default version still has not been migrated even in unstable.

Python/Ruby

Posted Feb 11, 2008 11:08 UTC (Mon) by drag (subscriber, #31333) [Link]

Python 2.5 is certainly a good thing. I am Debian user and habitually specify python2.5 when
working on my own stuff.

Hopefully Python 3000 will be even better. 

Python/Ruby

Posted Feb 11, 2008 14:43 UTC (Mon) by smitty_one_each (subscriber, #28989) [Link]

>language knowledge travels very poorly 
Ignorance is power, when deriving counter-arguments. ;)

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds