Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eaef615

Browse files
committed
As discussed on python-dev, changed builtin.zip() to handle zero arguments
by returning an empty list instead of raising a TypeError.
1 parent 9463792 commit eaef615

5 files changed

Lines changed: 20 additions & 11 deletions

File tree

Doc/lib/libfuncs.tex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,14 +1063,18 @@ \section{Built-in Functions \label{built-in-funcs}}
10631063
when the loop is usually terminated with \keyword{break}).
10641064
\end{funcdesc}
10651065

1066-
\begin{funcdesc}{zip}{seq1, \moreargs}
1066+
\begin{funcdesc}{zip}{\optional{seq1, \moreargs}}
10671067
This function returns a list of tuples, where the \var{i}-th tuple contains
1068-
the \var{i}-th element from each of the argument sequences. At
1069-
least one sequence is required, otherwise a \exception{TypeError} is
1070-
raised. The returned list is truncated in length to the length of
1068+
the \var{i}-th element from each of the argument sequences.
1069+
The returned list is truncated in length to the length of
10711070
the shortest argument sequence. When there are multiple argument
10721071
sequences which are all of the same length, \function{zip()} is
10731072
similar to \function{map()} with an initial argument of \code{None}.
10741073
With a single sequence argument, it returns a list of 1-tuples.
1074+
With no arguments, it returns an empty list.
10751075
\versionadded{2.0}
1076+
1077+
\versionchanged[Formerly, \function{zip()} required at least one argument
1078+
and \code{zip()} raised a \exception{TypeError} instead of returning
1079+
\code{[]}]{2.4}
10761080
\end{funcdesc}

Lib/test/test_builtin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@ def __getitem__(self, i):
11261126
if i < 0 or i > 2: raise IndexError
11271127
return i + 4
11281128
self.assertEqual(zip(a, I()), t)
1129-
self.assertRaises(TypeError, zip)
1129+
self.assertEqual(zip(), [])
1130+
self.assertEqual(zip(*[]), [])
11301131
self.assertRaises(TypeError, zip, None)
11311132
class G:
11321133
pass

Lib/test/test_iter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ def test_builtin_map(self):
423423

424424
# Test zip()'s use of iterators.
425425
def test_builtin_zip(self):
426-
self.assertRaises(TypeError, zip)
426+
self.assertEqual(zip(), [])
427+
self.assertEqual(zip(*[]), [])
428+
self.assertEqual(zip(*[(1, 2), 'ab']), [(1, 'a'), (2, 'b')])
429+
427430
self.assertRaises(TypeError, zip, None)
428431
self.assertRaises(TypeError, zip, range(10), 42)
429432
self.assertRaises(TypeError, zip, range(10), zip)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- zip() with no arguments now returns an empty list instead of raising
16+
a TypeError exception.
17+
1518
Extension modules
1619
-----------------
1720

Python/bltinmodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,11 +1916,9 @@ builtin_zip(PyObject *self, PyObject *args)
19161916
PyObject *itlist; /* tuple of iterators */
19171917
int len; /* guess at result length */
19181918

1919-
if (itemsize < 1) {
1920-
PyErr_SetString(PyExc_TypeError,
1921-
"zip() requires at least one sequence");
1922-
return NULL;
1923-
}
1919+
if (itemsize == 0)
1920+
return PyList_New(0);
1921+
19241922
/* args must be a tuple */
19251923
assert(PyTuple_Check(args));
19261924

0 commit comments

Comments
 (0)