module with __call__ defined is not callable?

adam johnson adam.sven.johnson at gmail.com
Tue Feb 7 22:24:34 EST 2006


Thanks for you answer.

I was under the impression that you could tack methods onto an object at any
time, your example almost works with old style classes and would with a
function instead of a method.

>>> class A:
...     def __init__(self):
...             self.__call__ = A.hello
...     def hello(self):
...             print "Hello, world!"
...
>>> a = A()
>>> a()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method hello() must be called with A instance as first
argument (got nothing instead)
>>> a(a)
Hello, world!
>>>

So now all I need to know is why now with new style classes the special
functions need to be defined on the class instead of attached to the
instance at any time.


On 08/02/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'>.
>
> Tim Delaney
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060208/c925320c/attachment.html>


More information about the Python-list mailing list