[Python-checkins] python/nondist/peps pep-0201.txt,1.19,1.20

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Fri, 01 Aug 2003 23:32:14 -0700


Update of /cvsroot/python/python/nondist/peps
In directory sc8-pr-cvs1:/tmp/cvs-serv29378

Modified Files:
	pep-0201.txt 
Log Message:
* Document the reasoning for having zip(*[]) return [] rather than raising
  an exception.

* Add notes about the itertools module addressing other concerns listed in
  the pep.



Index: pep-0201.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0201.txt,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** pep-0201.txt	14 Aug 2001 15:45:26 -0000	1.19
--- pep-0201.txt	2 Aug 2003 06:32:12 -0000	1.20
***************
*** 216,219 ****
--- 216,262 ----
        more detailed analysis, see version 1.7 of this PEP.
  
+ Subsequent Change to zip()
+ 
+     In Python 2.4, zip() with no arguments was modified to return an
+     empty list rather than raising a TypeError exception.  The rationale
+     for the original behavior was that the absence of arguments was
+     thought to indicate a programming error.  However, that thinking
+     did not anticipate the use of zip() with the * operator for unpacking
+     variable length argument lists.  For example, the inverse of zip
+     could be defined as:  unzip = lambda s: zip(*s).  That transformation
+     also defines a matrix transpose or an equivalent row/column swap for
+     tables defined as lists of tuples.  The latter transformation is
+     commonly used when reading data files with records as rows and fields
+     as columns.  For example, the code:
+ 
+         date, rain, high, low = zip(*csv.reader(file("weather.csv")))
+ 
+     rearranges columnar data so that each field is collected into
+     individual tuples for straight-forward looping and summarization:
+ 
+         print "Total rainfall", sum(rain)
+ 
+     Using zip(*args) is more easily coded if zip(*[]) is handled as an
+     allowable case rather than an exception.  This is especially helpful
+     when data is either built up from or recursed down to a null case
+     with no records.
+ 
+     Seeing this possibility, the BDFL agreed (with some misgivings) to
+     have the behavior changed for Py2.4.
+ 
+ Other Changes
+ 
+     - The xzip() function discussed above was implemented in Py2.3 in
+       the itertools module as itertools.izip().  This function provides
+       lazy behavior, consuming single elements and producing a single
+       tuple on each pass.  The "just-in-time" style saves memory and
+       runs faster than its list based counterpart, zip().
+ 
+     - The itertools module also added itertools.repeat() and
+       itertools.chain().  These tools can be used together to pad
+       sequences with None (to match the behavior of map(None, seqn)):
+ 
+           zip(firstseq, chain(secondseq, repeat(None)))
+ 
  
  References