[Python-Dev] RE: list comprehensions (was parsers and compilers for 2.0) (original) (raw)

Thomas Wouters thomas@xs4all.net
Sun, 13 Aug 2000 09:53:57 +0200


On Sat, Aug 12, 2000 at 09:33:43PM -0400, Tim Peters wrote:

[ ESR and Mark griping about list comprehensions syntax, which I can relate to, so I'll bother to try and exlain what bothers me wrt list comprehensions. Needn't be the same as what bothers them, though ]

List comprehensions are one of the best-loved features of Haskell (really!), and Greg/Skip/Ping's patch implements as an exact a parallel to Haskell's syntax and semantics as is possible in Python.

I don't see "it's cool in language X" as a particular good reason to include a feature... We don't add special syntax for regular expressions, support for continuations or direct access to hardware because of that, do we ?

Predictions of doom thus need to make a plausible case for why a rousing success in Haskell is going to be a disaster in Python. The only basis I can see for such a claim (and I have to make one up myself because nobody else has ) is that Haskell is lazy, while Python is eager. I can't get from there to "disaster", though, or even "plausible regret".

My main beef with the syntax is that it is, in my eyes, unpythonic. It has an alien, forced feel to it, much more so than the 'evil' map/filter/reduce. It doesn't 'fit' into Python the way most of the other features do; it's simply syntactic sugar for a specific kind of for-loop. It doesn't add any extra functionality, and for that large a syntactic change, I guess that scares me.

Those doubts were why I was glad you were going to write the PEP. I was looking forward to you explaining why I had those doubts and giving sound arguments against them :-)

Beyond that, Guido dislikes the way Lisp spells most things, so it's this or nothing. I'm certain I'll use it, and with joy. Do an update and try it.

Oh, I've tried it. It's not included in the 'heavily patched Python 2.0b1' I have running on a couple of machines to impress my colleagues, (which includes the obmalloc patch, augmented assignment, range literals, import as, indexing-for, and extended-slicing-on-lists) but that's mostly because I was expecting, like ESR, a huge debate on its syntax. Lets say that most my doubts arose after playing with it for a while. I fear people will start using it in odd construct, and in odd ways, expecting other aspects of for-loops to be included in list comprehensions (break, else, continue, etc.) And then there's the way it's hard to parse because of the lack of punctuation in it:

[((a,b)*c, (spam(d)%34)^e) for a in [(x, y) for x in L for y in S] for b in [b for b in B if mean(b)] for b,c in C for a,d in D for e in [Egg(a, b, c, d, e) for e in E]]

I hope anyone writing something like that (notice the shadowing of some of the outer vrbls in the inner loops) will either add some newlines and indentation by themselves, or will be hunted down and shot (or at least winged) by the PSU.

I'm not arguing to remove list comprehensions. I think they are cool features that can replace map/filter, I just don't think they're that much better than the use of map/filter.

Write-that-PEP-Tim-it-will-look-good-on-your-resume-ly y'rs,

Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!