[Tutor] random.choice()

Dick Moores rdm at rcblue.com
Wed Jul 2 17:50:48 CEST 2008

At 03:46 AM 7/2/2008, Kent Johnson wrote:
>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.


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?


>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list