[Python-Dev] zip() in Py2.4
Raymond Hettinger
Raymond Hettinger" <python@rcn.com
Wed, 30 Jul 2003 09:00:46 -0400
Currently, zip() with no arguments raises an exception.
Does anyone have objections to my having it return an
empty list instead?
A poster on comp.lang.python raised some valid objections
to the current behavior and I see no use case for wanting
to have an exception instead of a useful return value.
zip() --> [] would make the zip function more useful and
predictable when used with variable length argument lists:
def transpose(matrix):
return zip(*matrix)
On python-dev, when other features have been implemented,
there have been discussions about whether zero cases should
raise an exception or have a return value that is a more natural
extension of the non-zero cases. I believe the resolution was
typically to choose the latter (i.e. '' in 'abc'). This allows the
patterns to extend more naturally:
>>> zip('abc', range(3))
[('a', 0), ('b', 1), ('c', 2)]
>>> zip('abc')
[('a',), ('b',), ('c',)]
>>> zip()
[]
FWIW, I've tried it out and found that it only touches a couple
of lines of C, it breaks no test cases other than the two which
serve to verify the current behavior; my existing apps and
downloaded python apps all run fine. IOW, while it is a
backwards incompatible change, I do not expect to find
more than a modicum of existing code relying on an
exception from zip() with no arguments.
Another nice side benefit is that it simplifies the documentation
and makes zip() easier to learn in the first place. Code surrounding
a zip(*args) becomes more clear without a surrounding try/except.
Avoiding unnecessary exceptions is part of the reason for methods
like dict.get(k) which make coding a little more straight-forward.
Raymond Hettinger