Lambda going out of fashion

Andrew Dalke dalke at dalkescientific.com
Fri Dec 24 17:39:05 EST 2004


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
>>> 

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

Is that difference a bug?

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list