Lambda going out of fashion

Nick Coghlan ncoghlan at iinet.net.au
Sat Dec 25 08:04:40 EST 2004


Python 2.4 interactive session:
Py> class Blah:
...   def __iter__(self):
...     yield "Test"
...
Py> args = Blah()
Py> apply(len, args)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: apply() arg 2 expected sequence, found instance
Py> len(*args)
4
Py> class Blah(object):
...   def __iter__(self):
...     yield "Test"
...
Py> args = Blah()
Py> apply(len, args)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: apply() arg 2 expected sequence, found Blah
Py> len(*args)
4


And you're right, there is a behavioural difference - apply() expects a real 
sequence, whereas the extended function call syntax accepts any iterable.

However, there's also a bug in your demo code:

Py> def YieldTest():
...   yield "Test"
...
Py> x = YieldTest().__iter__
Py> list(x())
['Test']
Py> list(x())
[]

Your failing case with len(*args) was due to the iterator having already been 
consumed :)

Cheers,
Nick.
-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list