|
|
Subscribe / Log in / New account

Python/Ruby

Python/Ruby

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

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.


to post comments

Python/Ruby

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

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 (guest, #493) [Link] (2 responses)

> 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 (guest, #23124) [Link] (1 responses)

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 (guest, #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 © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds