* 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.
This commit is contained in:
Raymond Hettinger 2003-08-02 06:32:12 +00:00
parent 3cdcf4baf3
commit 6e249d8955
1 changed files with 43 additions and 0 deletions

View File

@ -215,6 +215,49 @@ BDFL Pronouncements
API and implementation, this elaboration is rejected. For a
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