Python Runtime Method Call Binding

Diez B. Roggisch deets at nospam.web.de
Thu Dec 4 09:58:38 EST 2008


k3xji wrote:

> Hi,
> 
> Is there a way to hook a function call in python? I know __getattr__
> is doing for variables, it is giving us a chance before a field is
> initialized. Do we have same functionality for methods?
> 
> Example:
> 
> class Foo(object):
>     def __call_method__(self, ...) # just pseudo
>         print 'A method is called in object...'
> 
> f = Foo()
> f.test_method()
> 
> I think you understand my point.

The special method __call__ is your friend.


class SomethingCallable(object):

   def __call__(self):
      print "I'm called, and you not!"

SomethingCallable()()

Diez



More information about the Python-list mailing list