Counting the number of call of a function

Laurent Claessens moky.math at gmail.com
Thu Sep 29 13:08:52 EDT 2011


    Hello


    Is it possible to count the number of time a function is called ?
Of course, if I've access to the source code, it's easy.

I tried the following :

def foo():
     print "foo !"


class wraper(object):
     def __init__(self,fun):
         globals()[fun]=self.replacement
     def replacement(*args):
         print "I'm replaced"

foo()
X=wraper(foo)
foo()

I was hoping that globals()[foo] would be replaced by my X.replacement 
and thus the second call to foo() was expected to print "I'm replaced".

Instead nothing is done.

By the way, I tried to print globals() inside __init__() to see what 
happens. It turns out that the entry 'foo' is never modified.

Any idea ?
I fact what I have to do is to add a decorator _a posteriori_ ...

Have a good night
Laurent






More information about the Python-list mailing list