[ x for x in xrange(10) when p(x) ]
Bengt Richter
bokr at oz.net
Sat Nov 12 05:27:16 EST 2005
On Sat, 12 Nov 2005 08:12:43 +0100, Peter Otten <__peter__ at web.de> wrote:
>bonono at gmail.com wrote:
>
>> However, I found something interesting which I don't quite understand :
>>
>> list((x for x in [1,2,3] if x<2 or stop())) works
>>
>> but
>>
>> a = [ x for x in [1,2,3] if x <2 or stop() ] doesn't.
>
>Here's how Carl Banks explained it to me when Bengt came up with this
>trick.
>
>http://mail.python.org/pipermail/python-list/2005-April/274051.html
>
Thanks (I guess, or is guilt being pinned? ;-) for the attribution.
BTW, that thread is easier to follow on google:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/ca7ba6ca9d70d24c/fdcadd1e3eba61cd
But googling shows I wasn't the first to come up with iter([]).next() to raise Stopiteration. Usual suspect:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/41495e4f5d3e1c2a/84c158ff18d08ece#84c158ff18d08ece
It probably stuck subconsciously in my mind and just came back when I wanted a StopIteration expression
for terminating a generator expression.
Regards,
Bengt Richter
More information about the Python-list
mailing list