LWN.net Logo

Python/Ruby

Python/Ruby

Posted Feb 10, 2008 21:30 UTC (Sun) by jmtapio (subscriber, #23124)
In reply to: Python/Ruby by rfunk
Parent article: Interview: Mark "Markey" Kretschmann (Not the Gentoo Weekly News)

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.)


(Log in to post comments)

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