Lambda going out of fashion
Andrew Dalke
dalke at dalkescientific.com
Sat Dec 25 07:25:25 EST 2004
Terry Reedy wrote:
> 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.
While true, the difference in error messages suggests that the
two approaches use slightly different and possibly exploitable
mechanisms.
> To be equivalent to len(*blah), this should be apply(len, blah), no *.
Oops. Here's one that really works. I have an idea of why
there's a difference but would rather someone explain it to me.
import traceback
def YieldTest():
yield "Test"
class Blah:
__len__ = None
__iter__ = YieldTest().__iter__
func = len
args = Blah()
try:
print "apply", apply(func, args)
except TypeError, err:
print "does not work:", err
try:
print "call", func(*args)
except TypeError, err:
print "does not work:", err
The output from running it under Python 2.3 is
apply 4
call does not work: len() takes exactly one argument (0 given)
If I make Blah be a new-style class (ie "class Blah(object):")
I get the opposite success behavior:
apply does not work: apply() arg 2 expected sequence, found Blah
call 4
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list