[Python-Dev] Please Vote -- Generator Comprehensions
Raymond Hettinger
python@rcn.com
Wed, 6 Mar 2002 13:23:10 -0500
This is a multi-part message in MIME format.
------=_NextPart_000_006B_01C1C512.0F85E180
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
After about 30 reviewers, the comments on PEP 279, Enhanced Generators, =
have slowed to a trickle.
The most universally liked part of the proposal is the addition of =
generator comprehensions.
No negative feedback was received on generator comprehensions; however, =
one person wanted to go further and add restartability; also, there was =
a little heartburn about why the existing list comprehensions expose =
their looping variable.
Before I send generator comprehension portion to Guido for =
pronouncement, I would like to get all of your votes +1 or -1 on just =
the part about Generator Comprehensions.
Everyone, please vote +1 or -1.=20
Thank you,
Raymond Hettinger
Here is the proposal in a nutshell:
If a list comprehension starts with a 'yield' keyword, then
express the comprehension with a generator. For example:
g =3D [yield (len(line),line) for line in file if len(line)>5]
print g.next()
This would be implemented as if it had been written:
def __tempGenComp(self):
for line in file:
if len(line) > 5:
yield (len(line), line)
g =3D __tempGenComp()
print g.next()
The full PEP is at http://python.sourceforge.net/peps/pep-0279.html.
The PEP has a full discussion of the restartability issue, an
analysis of why the loop variable should not be exposed,=20
and thoughts on why the syntax is appropriate. It represents
the collective wisdom of all the review comments submitted to-date.
------=_NextPart_000_006B_01C1C512.0F85E180
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4207.2601" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>After about 30 reviewers, the comments =
on PEP 279,=20
Enhanced Generators, have slowed to a trickle.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The most universally liked part of the =
proposal is=20
the addition of generator comprehensions.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>No negative feedback was received on =
generator=20
comprehensions; however, one person wanted to go further and add =
restartability;=20
also, </FONT><FONT face=3DArial size=3D2>there was a little =
heartburn about why=20
the existing list comprehensions expose their looping =
variable.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Before I send generator comprehension =
portion to=20
Guido for pronouncement, I would like to get all of your votes +1 or -1 =
on just=20
the part about Generator Comprehensions.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Everyone, please vote +1 or =
-1. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thank you,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Raymond Hettinger</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><A=20
href=3D"http://python.sourceforge.net/peps/pep-0279.html"></A></FONT>&nbs=
p;</DIV>
<DIV><FONT face=3DArial size=3D2>Here is the proposal in a =
nutshell:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2> If a list =
comprehension starts=20
with a 'yield' keyword, then<BR> express the =
comprehension=20
with a generator. For=20
example:<BR><BR> g =3D [yield=20
(len(line),line) for line in file if=20
len(line)>5]<BR> print=20
g.next()<BR><BR> This would be implemented as if it =
had been=20
written:<BR><BR> def=20
__tempGenComp(self):<BR> &=
nbsp; =20
for line in=20
file:<BR> &nbs=
p; =20
if len(line) >=20
5:<BR> &=
nbsp; =20
yield (len(line), line)<BR> g =
=3D=20
__tempGenComp()<BR> print=20
g.next()<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>The full PEP is at </FONT><A=20
href=3D"http://python.sourceforge.net/peps/pep-0279.html"><FONT =
face=3DArial=20
size=3D2>http://python.sourceforge.net/peps/pep-0279.html</FONT></A><FONT=
=20
face=3DArial size=3D2>.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The PEP has a full discussion of the =
restartability=20
issue, an</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>analysis of why the loop variable =
should not be=20
exposed, </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and thoughts on why the =
syntax is=20
appropriate.</FONT><FONT face=3DArial size=3D2> It =
represents</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>the collective wisdom of all the review =
comments=20
submitted to-date.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV></BODY></HTML>
------=_NextPart_000_006B_01C1C512.0F85E180--