[Python-ideas] while conditional in list comprehension ?? (original) (raw)

Shane Green shane at umbrellacode.com
Tue Jan 29 15:45:14 CET 2013


Here's what I was doing, and worked when i switched to the generator:

def stop(): … raise StopIteration()

list(((x if x < 5 else stop()) for x in range(10))) [0, 1, 2, 3, 4]

Shane Green www.umbrellacode.com 408-692-4666 | shane at umbrellacode.com

On Jan 29, 2013, at 6:36 AM, Wolfgang Maier <wolfgang.maier at biologie.uni-freiburg.de> wrote:

Are you trying to say you entered that code and it ran? I would be very surprised: if you could simply 'raise StopIteration' within the 'if' clause then there would be no point to the discussion. But as it is, your StopIteration should not be caught by the 'for', but will be raised directly. Did you try running it? Sorry, I missed your enclosing list(), which explains things of course. Cheers, Wolfgang


Python-ideas mailing list Python-ideas at python.org http://mail.python.org/mailman/listinfo/python-ideas

-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130129/9a2d6c9e/attachment.html>



More information about the Python-ideas mailing list