[Python-3000] Python 3000 Status Update (Long!)
Nick Coghlan
ncoghlan at gmail.com
Fri Jun 22 11:11:14 CEST 2007
Bill Janssen wrote:
>>> It should amount to "map(+, operands)".
>> Or, to be pedantic, this:
>>
>> reduce(lambda x, y: x.__add__(y), operands)
>
> Don't you mean:
>
> reduce(lambda x, y: x.__add__(y), operands[1:], operands[0])
This is a nice illustration of a fairly significant issue with the
usability of reduce: two attempts to rewrite sum() using reduce(), and
both of them are buggy. Neither of the solutions above can correctly
handle an empty sequence:
.>>> operands = []
.>>> reduce(lambda x, y: x.__add__(y), operands).
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: reduce() of empty sequence with no initial value
.>>> reduce(lambda x, y: x.__add__(y), operands[1:], operands[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list