reduce() anomaly?

Martin Chilvers martin at enthought.com
Wed Nov 5 19:42:09 CET 2003


Untested, but try this:-

	reduce(lambda x, y: x.update(y) or x, l, {})

The thing you are missing is that the result of x.update is None...

Martin



> -----Original Message-----
> From: python-list-bounces+martin=enthought.com at python.org 
> [mailto:python-list-bounces+martin=enthought.com at python.org] 
> On Behalf Of Stephen C. Waterbury
> Sent: Wednesday, November 05, 2003 11:18 AM
> To: python list
> Subject: reduce() anomaly?
> 
> 
> This seems like it ought to work, according to the
> description of reduce(), but it doesn't.  Is this
> a bug, or am I missing something?
> 
> Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
> [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
> Type "help", "copyright", "credits" or "license" for more 
> information.  >>> d1 = {'a':1}  >>> d2 = {'b':2}  >>> d3 = 
> {'c':3}  >>> l = [d1, d2, d3]  >>> d4 = reduce(lambda x, y: 
> x.update(y), l) Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>    File "<stdin>", line 1, in <lambda>
> AttributeError: 'NoneType' object has no attribute 'update'
>  >>> d4 = reduce(lambda x, y: x.update(y), l, {})
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>    File "<stdin>", line 1, in <lambda>
> AttributeError: 'NoneType' object has no attribute 'update'
> 
> - Steve.
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 







More information about the Python-list mailing list