module with __call__ defined is not callable?

limodou limodou at gmail.com
Tue Feb 7 22:09:58 EST 2006


On 2/8/06, Delaney, Timothy (Tim) <tdelaney at avaya.com> wrote:
> adam johnson wrote:
>
> > Hi All.
> > I was wondering why defining a __call__ attribute for a module
> > doesn't make it actually callable.
>
> For the same reason that the following doesn't work
>
>     class A (object):
>
>         def __init__(self):
>             self.__call__ = A.hello
>
>         def hello (self):
>             print 'Hello, world!'
>
>     a = A()
>     a()
>
>     Traceback (most recent call last):
>       File "D:\Python\modules\testclasscall.py", line 10, in ?
>         a()
>     TypeError: 'A' object is not callable
>
> The __call__ attribute must be defined on the class (or type) - not on
> the instance. A module is an instance of <type 'module'>.
>

A very easy example:

 >>> class A:
 ...     def __call__(self):
 ...         print 'call'
 ...
 >>> a = A()
 >>> a()
 call

I'm not sure that module can also has __call__() method just like
class. I havn't seen the way before.

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit



More information about the Python-list mailing list