newbie question, when __call__ method is used?

KefX keflimarcusx at aol.comNOSPAM
Mon Oct 27 00:26:31 EST 2003


>I have read other's code, and found it contains the __call__. But I
>don't know when its code will be executed.

It will be executed when a class instance is executed as a function.

For example:
foobar = MyClass()
foobar(2)

This is the same as:
foobar = MyClass()
foobar.__call__(2)

One way of looking at it is that it's simple shorthand, but there's also
another advantage (which is the real reason it's there): it turns your class
instance into a 'callable', that is, almost anything that requires a function
can also take your class instance (in general).

- Kef




More information about the Python-list mailing list