Count nb call of a function, without global var or decorator
Duncan Booth
duncan.booth at invalid.invalid
Tue Feb 6 03:26:44 EST 2007
"Méta-MCI" <enleverlesX.XmcX at XmclaveauX.com> wrote:
> Example, with meta-data (attributs of function) :
>
> def ff(this):
> try:
> this.count=this.count+1
> except:
> this.count=1
> a=1
> b=2
> c=a+b
>
> ff(ff)
> fa=ff
> ff(ff)
> fa(fa)
> print ff.count
>
>
>
> How to improve that?
If I've managed to guess what you are asking, you want to use a class:
>>> class Ff:
def __init__(self):
self.count = 0
def __call__(self, notused):
self.count += 1
a, b = 1, 2
c = a+b
return c
>>> ff = Ff()
>>> fa = Ff()
>>> ff(ff)
3
>>> ff.count
1
>>> fa.count
0
More information about the Python-list
mailing list