Lambda going out of fashion

Benji York benji at benjiyork.com
Sat Dec 25 00:38:57 CET 2004


Andrew Dalke wrote:
> Terry Reedy wrote:
> 
>>As far as I know, apply(func, args) is exactly equivalent to func(*args).
> 
> 
> After playing around a bit I did find one difference in
> the errors they can create.
> 
> 
>>>>def count():
> 
> ...   yield 1
> ... 
> 
>>>>apply(f, count())
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: apply() arg 2 expected sequence, found generator
> 
>>>>f(*count())
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: len() of unsized object

No question that they report different errors in the face of being given 
unsupported input.


> That led me to the following
<snip>

>>>>blah = Blah()
>>>>len(*blah)
> 
> 6
> 
>>>>apply(len, *blah)
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: len() takes exactly one argument (6 given)
> 
> 
> Is that difference a bug?

They do two different things.  I think you have a spurious * in the call 
to apply.  The apply above is equivalent to

 >>> len('H', 'e', 'l', 'l', 'o', '!')

Which gives the same error:

Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: len() takes exactly one argument (6 given)

If the * is removed, it works correctly:

 >>> apply(len, blah)
6
-- 
Benji York
benji at benjiyork.com




More information about the Python-list mailing list