Counting the number of call of a function

Chris Angelico rosuav at gmail.com
Thu Sep 29 19:22:12 CEST 2011


On Fri, Sep 30, 2011 at 3:08 AM, Laurent Claessens <moky.math at gmail.com> wrote:
> 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".

Actually, I've just realized what your problem might be. Try:
X = wraper("foo")

You're indexing globals() with the actual function object, but you
want to index it with the function _name_. I do think that the
decorator technique will be a lot cleaner, though.

ChrisA



More information about the Python-list mailing list