__call__

tiissa tiissa at nonfree.fr
Sat May 28 16:13:02 EDT 2005


TK wrote:
> Sorry but it does not work.

It _does_ work. It just don't do what you expect. That's different till 
you know how to do it.

Let's see:

 >  >>> class Test(object):
 > ...     def __call__(self):
 > ...         print 'Hi'
 > ...

You first define a class whose members can be called.

 >  >>> Test()
 > <__main__.Test object at 0x3e6d0>

Then you build an instance of these class. Fine.

If you want to call this instance you have to tell python:

 >>> class Test:
...     def __init__(self):
...             print 'In init.'
...     def __call__(self):
...             print 'In call.'
...
 >>> t=Test()
In init.
 >>> t()
In call.
 >>> Test()
In init.
<__main__.Test instance at 0x401e2b6c>
 >>> Test()()
In init.
In call.
 >>>



More information about the Python-list mailing list