Re: __call__
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: __call__

From: tiissa <tiissa@nonfree.fr>
Date: Sat May 28 2005 - 22:13:02 CEST

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.
>>>
Received on Thu Sep 29 16:15:38 2005