[Python-3000] callable()
Andrew Koenig
ark-mlist at att.net
Tue Jul 18 20:24:21 CEST 2006
> Agreed. I think the people who want to use this as a test for whether
> a client passed them a usable object are barking up the wrong tree.
> What I do see it as useful for is making an api that accepts a
> foo-like-object, or a callable object that returns a foo-like-object.
Yes. What really got me started down this particular line of reasoning was
Snobol4's notion (from 1968!) of an "unevaluated expression," (indicated by
unary *) which was like a function with no arguments. A quick example:
A = *(B + C)
B = 4
C = 5
OUTPUT = EVAL(A)
This code fragment prints 9. The Python analogy:
a = (lambda: b + c)
b = 4
c = 5
print a()
Now, there are some contexts (particularly in pattern matching) that accept
either an unevaluated expression or an ordinary value, and if they get an
unevaluated expression, they evaluate it at the last possible instant during
pattern matching.
Imagine a program that is trying to build a data structure out of values,
some of which might be unevaluated expressions. There are various
optimizations you can do if you know that you are dealing with an actual
value at the moment; so you would like to be able to determine whether
something is an unevaluated expression so you can decide whether to attempt
the optimizations.
So you need a way to tell whether something is an unevaluated expression
even though it's not yet time to evaluate it.
If we use Python functions to represent unevaluated expressions, this desire
leads naturally to a way to tell whether a value is a function. Of course
we could solve the problem by using an instance of a specific class instead,
but I feel that's just adding complexity without correspondingly adding
utility.
More information about the Python-3000
mailing list