[ x for x in xrange(10) when p(x) ]

bonono at gmail.com bonono at gmail.com
Thu Nov 10 10:41:51 EST 2005


Alex Martelli wrote:
> This is the first time on this thread in which I'm glimpsing that you
> mean 'when' not as in SQL (where it has just the same meaning as the
> 'if' in Python's genexps/listcomps), but rather with the meaning that
> any Pythonista would instinctively spell 'while'.  Since AFAIK 'when' is
> only used in SQL (out of widespread languages), using it with a
> drastically different meaning would be an utter disaster, IMHO.
The original post was in the context about dropwhile/takewhile. I may
choose the wrong word "when". BTW, what does "when" do it SQL ? I only
know about "where" which I believe is the closest equvialent of "if" in
python generator comprehension/list expression.

To me when/while has a "sequence" element there whereas "where/if"
don't. I didn't choose "while" because I know it is a key word already
in python so I never thought about it being in this kind of construct,
but thinking about it, "if" can be used in list expression  then may be
while can be used to.

>
> Right now, listcomps and genexps can be explained very simply as
> equivalent to just the same nesting of for and if statement as they have
> clauses in sequence.  Adding a 'while' clause (or 'until', etc) would
> unfortunately break this simple rule, and therefore make the whole
> construct harder, not easier, to understand.  Unless there are very
> compelling and frequent use cases for such an addition, I doubt it's
> worth even trying to make a patch in order to time it against
> itertools.takewhile...
I have no idea what it is involved or what about rules. I just raised a
simple question because the whole dropping filter/map/reduce discussion
I read gave me the impression that python is siding over in-line
list/generator expression rather than the more traditional
map/reduce/takewhile function call style.




More information about the Python-list mailing list