[Python-checkins] CVS: python/nondist/peps pep-0201.txt,1.2,1.3
Barry Warsaw
python-dev@python.org
Tue, 18 Jul 2000 21:19:57 -0700
Update of /cvsroot/python/python/nondist/peps
In directory slayer.i.sourceforge.net:/tmp/cvs-serv10215
Modified Files:
pep-0201.txt
Log Message:
In the examples section, show how zip() is reversible.
Patches to the reference implementation:
__getitem__() raises IndexError immediately if no sequences were
given.
__len__() returns 0 if no sequences were given.
__cmp__() new method
Added a little more explanation to raise-a-TypeError-for-zip(a)
Added `fold' as one of the alternative names proposed.
Index: pep-0201.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0201.txt,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** pep-0201.txt 2000/07/17 18:49:21 1.2
--- pep-0201.txt 2000/07/19 04:19:54 1.3
***************
*** 173,177 ****
--- 173,197 ----
[(1, 5, 9, 12), (2, 6, 10, 13), (3, 7, 11, None), (4, 8, None, None)]
+ Note that when the sequences are of the same length, zip() is
+ reversible:
+ >>> a = (1, 2, 3)
+ >>> b = (4, 5, 6)
+ >>> x = zip(a, b)
+ >>> y = zip(*x) # alternatively, apply(zip, x)
+ >>> z = zip(*y) # alternatively, apply(zip, y)
+ >>> x
+ [(1, 4), (2, 5), (3, 6)]
+ >>> y
+ [(1, 2, 3), (4, 5, 6)]
+ >>> z
+ [(1, 4), (2, 5), (3, 6)]
+ >>> x == z
+ 1
+
+ It is not possible to reverse zip this way when the sequences are
+ not all the same length.
+
+
Reference Implementation
***************
*** 196,199 ****
--- 216,221 ----
def __getitem__(self, i):
+ if not self.__sequences:
+ raise IndexError
ret = []
exhausted = 0
***************
*** 219,222 ****
--- 241,246 ----
if shortest < 0 or slen < shortest:
shortest = slen
+ if shortest < 0:
+ return 0
return shortest
longest = 0
***************
*** 227,230 ****
--- 251,278 ----
return longest
+ def __cmp__(self, other):
+ i = 0
+ smore = 1
+ omore = 1
+ while 1:
+ try:
+ si = self[i]
+ except IndexError:
+ smore = 0
+ try:
+ oi = other[i]
+ except IndexError:
+ omore = 0
+ if not smore and not omore:
+ return 0
+ elif not smore:
+ return -1
+ elif not omore:
+ return 1
+ test = cmp(si, oi)
+ if test:
+ return test
+ i = i + 1
+
def __str__(self):
ret = []
***************
*** 336,340 ****
3) Raises TypeError
! Pros: None
Cons: needless restriction
--- 384,389 ----
3) Raises TypeError
! Pros: zip(a) doesn't make much sense and could be confusing
! to explain.
Cons: needless restriction
***************
*** 346,352 ****
(but are not limited to!): marry, weave, parallel, lace, braid,
interlace, permute, furl, tuples, lists, stitch, collate, knit,
! plait, and with. All have disadvantages, and there is no clear
! unanimous choice, therefore the decision was made to go with
! `zip' because the same functionality is available in other
languages (e.g. Haskell) under the name `zip'[2].
--- 395,401 ----
(but are not limited to!): marry, weave, parallel, lace, braid,
interlace, permute, furl, tuples, lists, stitch, collate, knit,
! plait, fold, and with. All have disadvantages, and there is no
! clear unanimous choice, therefore the decision was made to go
! with `zip' because the same functionality is available in other
languages (e.g. Haskell) under the name `zip'[2].