[Tutor] random.choice()

Kent Johnson kent37 at tds.net
Wed Jul 2 12:46:45 CEST 2008

On Wed, Jul 2, 2008 at 3:54 AM, wesley chun <wescpy at gmail.com> 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.


More information about the Tutor mailing list