Next / Previous / Contents / TCC Help System / NM Tech homepage

26.3.5. __call__(): What to do when someone calls an instance

If a class has a .__call__() method, its instances can be called as if they were functions.

Any arguments passed in that function call become arguments to the .__call__() method. Example:

>>> class CallMe(object):
...     def __call__(self, *p, **k):
...         print "CallMe instance called with:"
...         print "Positional arguments", p
...         print "Keyword arguments", k
... 
>>> c=CallMe()
>>> c(1, 'rabbit', fetchez='la vache', hamster='elderberries')
CallMe instance called with:
Positional arguments (1, 'rabbit')
Keyword arguments {'fetchez': 'la vache', 'hamster': 'elderberries'}