[Python-checkins] peps: Updated based on Guido's recent pronouncements on the Open Issues. (original) (raw)

georg.brandl python-checkins at python.org
Wed Mar 23 21:23:35 CET 2011


http://hg.python.org/peps/rev/e4678d0f2cbb changeset: 55:e4678d0f2cbb user: Barry Warsaw <barry at python.org> date: Thu Jul 27 19:15:20 2000 +0000 summary: Updated based on Guido's recent pronouncements on the Open Issues. There are now no more open issues.

files: pep-0201.txt | 302 ++++++-------------------------------- 1 files changed, 50 insertions(+), 252 deletions(-)

diff --git a/pep-0201.txt b/pep-0201.txt --- a/pep-0201.txt +++ b/pep-0201.txt @@ -84,31 +84,17 @@ generator function, available in the builtin module. This function is to be called zip' and has the following signature: - zip(seqa, [seqb, [...]], [pad=<value>]) + zip(seqa, [seqb, [...]]) zip() takes one or more sequences and weaves their elements together, just as map(None, ...) does with sequences of equal - length. The optional keyword argument pad', if supplied, is a - value used to pad all shorter sequences to the length of the - longest sequence. If `pad' is omitted, then weaving stops when - the shortest sequence is exhausted. + length. The weaving stops when the shortest sequence is + exhausted. - It is not possible to pad short lists with different pad values, - nor will zip() ever raise an exception with lists of different - lengths. To accomplish either behavior, the sequences must be - checked and processed before the call to zip() -- but see the Open - Issues below for more discussion. +Return Value -Lazy Execution

Examples @@ -127,23 +113,9 @@ >>> zip(a, d) [(1, 12), (2, 13)] - >>> zip(a, d, pad=0) - [(1, 12), (2, 13), (3, 0), (4, 0)]

@@ -171,235 +143,60 @@ built-in function and helper class. These would ultimately be replaced by equivalent C code. - class _Zipper: - def init(self, args, kws): - # Defaults - self.__padgiven = 0 - if kws.has_key('pad'): - self.__padgiven = 1 - self.__pad = kws['pad'] - del kws['pad'] - # Assert no unknown arguments are left - if kws: - raise TypeError('unexpected keyword arguments') - self.__sequences = args - self.__seqlen = len(args) + def zip(*args): + if not args: + raise TypeError('zip() expects one or more sequence arguments') + ret = [] + # find the length of the shortest sequence + shortest = min(*map(len, args)) + for i in range(shortest): + item = [] + for s in args: + item.append(s[i]) + ret.append(tuple(item)) + return ret - def getitem(self, i): - if not self.__sequences: - raise IndexError - ret = [] - exhausted = 0 - for s in self.__sequences: - try: - ret.append(s[i]) - except IndexError: - if not self.__padgiven: - raise - exhausted = exhausted + 1 - if exhausted == self.__seqlen: - raise - ret.append(self.__pad) - return tuple(ret) - def len(self): - # If we're padding, then len is the length of the longest sequence, - # otherwise it's the length of the shortest sequence. - if not self.__padgiven: - shortest = -1 - for s in self.__sequences: - slen = len(s) - if shortest < 0 or slen < shortest: - shortest = slen - if shortest < 0: - return 0 - return shortest - longest = 0 - for s in self.__sequences: - slen = len(s) - if slen > longest: - longest = slen - return longest +BDFL Pronouncements - 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 + Note: the BDFL refers to Guido van Rossum, Python's Benevolent + Dictator For Life. - def str(self): - ret = [] - i = 0 - while 1: - try: - ret.append(self[i]) - except IndexError: - break - i = i + 1 - return str(ret) - repr = str + - The function's name. An earlier version of this PEP included an + open issue listing 20+ proposed alternative names to zip(). In + the face of no overwhelmingly better choice, the BDFL strongly + prefers zip() due to it's Haskell[2] heritage. See version 1.7 + of this PEP for the list of alteratives. + - zip() shall be a built-in function. - def zip(*args, **kws): - return _Zipper(args, kws) + - Optional padding. An earlier version of this PEP proposed an + optional pad' keyword argument, which would be used when the + argument sequences were not the same length. This is similar + behavior to the map(None, ...) semantics except that the user + would be able to specify pad object. This has been rejected by + the BDFL in favor of always truncating to the shortest sequence. + - Lazy evaluation. An earlier version of this PEP proposed that + zip() return a built-in object that performed lazy evaluation + using __getitem__() protocol. This has been strongly rejected + by the BDFL in favor of returning a real Python list. If lazy + evaluation is desired in the future, the BDFL suggests an xzip() + function be added. -Rejected Elaborations + - zip() with no arguments. the BDFL strongly prefers this raise a + TypeError exception. - Some people have suggested that the user be able to specify the - type of the inner and outer containers for the zipped sequence. - This would be specified by additional keyword arguments to zip(), - named inner' and `outer'. + - zip() with one argument. the BDFL strongly prefers that this + return a list of 1-tuples. - This elaboration is rejected for several reasons. First, there - really is no outer container, even though there appears to be an - outer list container the example above. This is simply an - artifact of the repr() of the zipped object. User code can do its - own looping over the zipped object via getitem(), and build - any type of outer container for the fully evaluated, concrete - sequence. For example, to build a zipped object with lists as an - outer container, use

References @@ -409,6 +206,7 @@

 TBD: URL to python-dev archives

-- Repository URL: http://hg.python.org/peps



More information about the Python-checkins mailing list