At 03:46 AM 7/2/2008, Kent Johnson wrote:
On Wed, Jul 2, 2008 at 3:54 AM, wesley chun <[EMAIL PROTECTED]> wrote:
> in the former, you have a function object. it's just like any other
> Python object, but with one heaping distinction: it's callable --
> this means that u can slap on a pair of parentheses after the object
> and execute it, which is what i did after calling choice() above to
> pick one of the 2 functions, then *calling it* with the trailing "()".
A slight nit-pick - being callable is not such a huge distinction. Any
object whose class has a __call__() method is callable, including
functions, bound and unbound methods, and instances of user-defined
classes containing __call__(). For example:
In [6]: class Callable(object):
...: def __call__(self):
...: print "I'm not a function"
In [7]: c=Callable()
In [8]: c()
I'm not a function
Like so many things in Python, the mechanism underlying function call
is exposed through a special method and available to hook into.
Kent,
It seems to me that Wes is saying only that all function objects are
callable, not that all callable objects are functions. Are there
function objects that aren't callable? Your example is a callable
object that isn't a function, right?
Dick
Kent
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor