a small problem

Tim Peters tim_one at email.msn.com
Tue Jan 11 23:23:29 EST 2000


[knotwell at my-deja.com]
>> Is the following a bug?
>>
>> >>> q = [(1,'dd'),(2,'ddd'),(3,'ddddd')]
>>
>> >>> reduce(lambda x,y: x[0] + y[0],q)
>>
>> Traceback (innermost last):
>>   File "<stdin>", line 1, in ?
>>   File "<stdin>", line 1, in <lambda>
>> AttributeError: __getitem__

[Thomas Wouters, explains it, then ...]
> ...
> >>> reduce(lambda x,y: (x[0] + y[0],),q)
> (6,)
>
> or perhaps
>
> >>> reduce(lambda x,y: (x[0] + y[0],),q)[0]
> 6
>
> depending on what you want. But I think this is roughly the
> point where people like Guido(*) start wondering if reduce
> is a good idea at all. I'd say the above reduce is almost a
> good argument for perl ;) Usually, what you're trying to do
> so forcefully with just reduce and lambda is much more
> elegantly (and readably!) done with map and reduce (and
> lambda, in this case):
>
> >>> import operator
> >>> tmplist = map(lambda x: x[0], q)
> >>> tmplist
> [1, 2, 3]
> >>> reduce(operator.add, tmplist)
> 6

Whereas people like Guido <wink> would not have considered writing anything
other than

>>> sum = 0
>>> for n, s in q:
        sum = sum + n
>>>


> (This may seem like nitpicking for the above example, but it
> makes for readable code later on, when you write longer and
> longer and more complicated map/filter/reduce sequences.

People like Guido would agree, but would say it applies with three times the
force to the dirt-simple and dirt-obvious code just above.

Seriously, I don't think I've ever seen a good use for reduce in Python --
although I've certainly used it <wink>.

> ...
> (*) I'm assuming here. I dont actually know so, because
> i'm not foolish enough to consider myself knowledgable in
> all things Guido, but these are the hints i get from other
> postings on the subject, by other people.

My favorite line from Guido on this is available *somewhere* in DejaNews,
where I responded to someone going over the edge with these things that they
were intended to be minor conveniences, and Guido jumped in to amend that to
"minor nuisances".

In the Nov '98 issue of Linux Journal, there's an interview with Guido.  In
response to "What feature of Python are you least pleased with?",

    Guido:  Sometimes I've been too quick in accepting
    contributions, and later realized that it was a mistake.
    One example would be some of the functional programming
    features.  lambda is a keyword that lets you create a
    small anonymous function; built-in functions such as map,
    filter and reduce run a function over a sequence type,
    such as a list.

    In practice, it didn't turn out that well.  Python has
    only two scopes:  local and global.  This makes writing
    lambda functions painful, because you often want to
    access variables in the scope where the lambda was
    defined, but you can't because of the two scopes.  There's
    a way around this, but it's something of a kludge.  Often
    it seems much easier in Python just to use a for loop
    instead of messing around with lambda functions.  map
    and friends work well only when a built-in function that
    does what you want already exists.

I suppose finally posting that will rekindle a few old flamewars!  Brrrr.

There's another reason to look up this interview:  there's a great staged
photo of Andrew Kuchling and GvR "discussing the future of Python".  You can
tell it's staged because Andrew is paying attention, and Guido is holding a
coffee cup (with what appears to be a pencil floating in it) instead of his
usual chainsaw <wink>.

can't-believe-he'd-drink-coffee-with-a-straw-ly y'rs  - tim






More information about the Python-list mailing list