[Python-3000] Python 3000 Status Update (Long!)

Nicko van Someren nicko at nicko.org
Wed Jun 20 12:22:11 CEST 2007


On 19 Jun 2007, at 21:53, Mike Klaas wrote:
> ...
> Stats from _any_ large python project is better than anecdotes.
> Perhaps it would be better to turn to the stdlib (367289 lines)?
...
>        reduce(lambda a, b: (0, a[1] + b[1]), items)[1]
>
> (which could be written sum(x[1] for x in items)

Only if the items at index 1 happen to be numbers.  That's another  
bugbear of mine.  The sum(l) built-in is NOT equivalent to reduce 
(operator.add, l) in Python 2.x:
	>>> reduce(operator.add, [1,2,3])
	6
	>>> reduce(operator.add, ['a','b','c'])
	'abc'
	>>> reduce(operator.add, [["a"],[u'b'],[3]])
	['a', u'b', 3]
	>>> sum([1,2,3])
	6
	>>> sum(['a','b','c'])
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	TypeError: unsupported operand type(s) for +: 'int' and 'str'
	>>> sum([["a"],[u'b'],[3]])
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	TypeError: unsupported operand type(s) for +: 'int' and 'list'

Given that reduce is moving one step further away in Python 3, and  
given that it's use seems to be somewhat discouraged these days  
anyway, perhaps the sum() function could be made properly polymorphic  
so as to remove one more class of use cases for reduce().

	Nicko



More information about the Python-3000 mailing list