[Python-Dev] PEP 3142: Add a "while" clause to generator expressions

Vitor Bosshard algorias at yahoo.com
Tue Jan 20 17:32:24 CET 2009


----- Mensaje original ----
> De: "python-3000 at udmvt.ru" <python-3000 at udmvt.ru>
> Para: Gerald Britton <gerald.britton at gmail.com>
> CC: python-dev at python.org
> Enviado: martes, 20 de enero, 2009 11:18:24
> Asunto: Re: [Python-Dev] PEP 3142: Add a "while" clause to generator expressions
> 
> May I suggest you this variant?
> 
>     def raiseStopIteration():
>         raise StopIteration
> 
>     g = (n for n in range(100) if n*n < 50 or raiseStopIteration())
> 
> Well, there are more characters...
> 
> But it is not using any syntax changes and does not require any approval
> to be functional. Yet it is as fast as the proposed variant, does not require
> modules and, I hope, will not confuse you or anyone else.
> 

This works as a generator, but not as a list comprehension. The exception is propagated instead of just cutting short the loop:

>>> def r(): raise StopIteration
>>> print [i for i in range(10) if i**2 < 50 or r()]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print [i for i in range(10) if i**2 < 50 or r()]
  File "<pyshell#1>", line 1, in r
    def r(): raise StopIteration
StopIteration
>>> 


Vitor


      ¡Todo sobre la Liga Mexicana de fútbol! Estadisticas, resultados, calendario, fotos y más:&lt;
http://espanol.sports.yahoo.com/


More information about the Python-Dev mailing list