Lambda going out of fashion

Terry Reedy tjreedy at udel.edu
Fri Dec 24 21:25:10 EST 2004


"Andrew Dalke" <dalke at dalkescientific.com> wrote in message 
news:pan.2004.12.24.22.39.33.858597 at dalkescientific.com...
> 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.

Ok, add 'assuming that func and args are a valid callable and sequence 
respectively.'  Error messages are not part of the specs, and may change 
for the same code from version to version.

>>>> 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
>>>>
>
> That led me to the following
>
>>>> class Blah:
> ...   def __len__(self):
> ...     return 10
> ...   def __getitem__(self, i):
> ...     if i == 0: return "Hello!"
> ...     raise IndexError, i
> ...
>>>> blah = Blah()
>>>> len(*blah)
> 6
>>>> apply(len, *blah)

To be equivalent to len(*blah), this should be apply(len, blah), no *.

>>> apply(len, blah)
6 #Py2.2

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

In your input, yes ;-)

Terry J. Reedy







More information about the Python-list mailing list