[Python-Dev] Re: PEP 279

James Althoff jamescalthoff@yahoo.com
Thu, 4 Apr 2002 11:25:56 -0800


This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C1DBCB.7D512B00
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

[David Abrahams]
> . . . I want to put in another plug for itemize(),

Well, since we're doing post-Pronouncement fantasizing . . .

here's one more plug for "itemize".

Noting that

    for k,v in adict.iteritems():

is probably as useful as

    for i,v in enumerate(aseq):

why not change the spec to:

>>> def itemize(iterable):
...     # use iteritems if defined
...     try:
...         for item in iterable.iteritems(): yield item
...     except AttributeError: pass
...     # and then everything as before
...     i =3D 0
...     iterator =3D iter(iterable)
...     while 1:
...         yield i,iterator.next()
...         i +=3D 1
...

So "itemize(iterable)" returns an iterator that yields (k,v) pairs from =
the iterable.iteritems() method if defined, else pairs generated by =
associating 0,1,...,n-1 with n values from iter(iterable).

This allows

    for k,v in itemize(adict_or_aseq):

to be written uniformly for dicts and seqs.

And makes=20

    list(itemize(adict)) =3D=3D adict.items()

work, removing one of the objections to the name "itemize".

Also, if one were to need, for example, a sequence whose indices start =
at 1 instead of 0, one could define a  sequence class that implements =
iteritems and objects of said class would work just fine with "itemize".

Jim

------=_NextPart_000_0005_01C1DBCB.7D512B00
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 6.00.2715.400" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>[David Abrahams]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&gt; . . .&nbsp;I want to put in =
another plug for=20
itemize(),<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Well, since we're doing =
post-Pronouncement=20
fantasizing . . .</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT size=3D2>here's one more plug for=20
"itemize".</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Noting that</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; for k,v in=20
adict.iteritems():</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>is probably as useful as</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; for i,v in=20
enumerate(aseq):</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>why not c</FONT><FONT =
face=3DArial><FONT size=3D2>hange=20
the spec to:</FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; def=20
itemize(iterable):<BR>...&nbsp;&nbsp;&nbsp;&nbsp; # use iteritems if=20
defined<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=20
try:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for item in=20
iterable.iteritems(): yield item<BR>...&nbsp;&nbsp;&nbsp;&nbsp; except=20
AttributeError: pass<BR>...&nbsp;&nbsp;&nbsp;&nbsp; # and then =
everything as=20
before<BR>...&nbsp;&nbsp;&nbsp;&nbsp; i =3D =
0<BR>...&nbsp;&nbsp;&nbsp;&nbsp;=20
iterator =3D iter(iterable)<BR>...&nbsp;&nbsp;&nbsp;&nbsp; while=20
1:<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yield=20
i,iterator.next()<BR>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
i +=3D=20
1<BR>...<BR></FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT size=3D2><FONT>So =
"itemize(iterable)" returns an=20
iterator that yields (k,v) pairs from the iterable.iteritems() method if =

defined, else pairs generated by associating 0,1,...,n-1 with n values =
from=20
iter(iterable).</FONT></FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT><FONT size=3D2><FONT>This=20
allows</FONT></FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT><FONT size=3D2><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp; =
for k,v in=20
itemize(adict_or_aseq):</FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT><FONT size=3D2><FONT>to be written =
uniformly for dicts=20
and seqs.</FONT></FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>And makes </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; =
list(itemize(adict)) =3D=3D=20
adict.items()</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>work, removing one of the objections to =
the name=20
"itemize".</FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT><FONT size=3D2><FONT>Also, if one were to =
need, for=20
example,&nbsp;a sequence whose indices start at 1 instead of 0, one =
could define=20
a&nbsp; sequence class that implements iteritems and objects of said =
class would=20
work&nbsp;just fine&nbsp;with =
"itemize".</FONT></FONT></FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT></FONT></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT><FONT=20
size=3D2><FONT>Jim</DIV></FONT></FONT></FONT></FONT></BODY></HTML>

------=_NextPart_000_0005_01C1DBCB.7D512B00--