[Python-Dev] list comprehensions again...
Tue, 11 Jul 2000 23:47:38 +0200
On Tue, Jul 11, 2000 at 03:54:16PM -0400, Barry A. Warsaw wrote:
> Sorry, but I'm being to think that both the list comprehensions and
> parallel for loops are looking pretty dodgy. I wonder what newbies
> are going to think when they see this stuff? If the semantics aren't
> obvious to us, they definitely will not be to them.
> Range literals I can see as being worth it. I wonder if parallel for
> loops can't be handled more gracefully with a function -- isn't it
> just a nice wrapper around a specific call to map() anyway?
Well, not really. It doesn't create a new list, only a tuple to hold lists
to iter over (in order to pass it to the opcode as a single PyObject) and
similar for the resulting objects. It's definately more efficient than the
map() solution (and different, see below ;)
I agree that all this together looks dodgy, but I think there is a simple
way to view it... I still think
'for x in a; y in b:'
is the right syntax for parallel for loops. It's almost "only" syntactic
'for x,y in map(None, a, b)'
but it's fairly straightforward, and in large blocks of code it might look a
lot better. It also has the benifit of not padding the shortest list to the
size of the longest one: getting that behaviour with a map requires an
aditional 'filter' or such.
Whether it does or doesn't do what people actually expect I don't know, but
with all due respect to Greg for doing the usability test, I don't think it
carries that much meaning: We should be testing Python newbies, not people
who haven't tried Python at all, yet. Unfortunately, I don't have such a
batch at hand either ;) (My colleagues either refuse to learn Python, or
haven't stayed newbie for more than a few hours :-)
Given that you can already do the nested-for-loop with a nested for loop,
and it doesn't suffer (much) in way of readability or pythonity, I think
most people will realize that the parallel for loop does actually do a
parallel for-each. A number of Python features, like (the lack of) nested
namespaces and the way assignment works, seem counter-intuitive (or at least
lesser-intuitive) at first, but there simply isn't a better way to handle
them and maintain Python's simplicity. I think this is one of those cases.
Note that this doesn't include list comprehensions ! I find the syntax
confusing myself, but it doesn't look like we can fix it by stealing the
best syntax of parallel-for loops for it ;) Also, list comprehensions don't
add *that* much. They provide an easier way to create a list, but they don't
eradicate the need for map, reduce or filter, because there is no way to
comprehend an already created list.
I like Paul's proposal that makes list comprehensions look more like normal
'for' loops, requiring indentation and all, but I'm not sure if they're
going to work... This definately requires more thought ;-) I think I'll post
a (very) short summary of the entire discussion and some examples for people
to vote about, and perhaps express their opinion, on c.l.py. Sometime
Thomas Wouters <email@example.com>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!