[Tutor] Counting method calls
Ricardo Aráoz
ricaraoz at gmail.com
Sat Sep 22 13:50:38 CEST 2007
Hi, I'm trying to count method calls. Tried this but...
>>>> class MyList(list):
> ... def __init__(self):
> ... self.calls = 0
> ... def __getattr__(self, name):
> ... self.calls += 1
> ... return list.__getattribute__(self, name)
>
>>>> a = MyList()
>>>> a
> []
>>>> a.append(1)
>>>> a
> [1]
>>>> a.calls
> 88
>>>> a.append(3)
>>>> a.calls
> 88
>>>> a.sort()
>>>> a
> [1, 3]
>>>> a.calls
> 176
It's doing some strange things with self.calls.
I've also tried :
>>>> class MyList(list):
> ... def __init__(self):
> ... self.calls = 0
> ... def __getattribute__(self, name): # Here's the change
> ... self.calls += 1
> ... return list.__getattribute__(self, name)
>
>>>> a = MyList()
>>>> a
> []
>>>> a.append(1)
File "<input>", line 5, in __getattribute__
File "<input>", line 5, in __getattribute__
.... snipped .....
File "<input>", line 5, in __getattribute__
File "<input>", line 5, in __getattribute__
RuntimeError: maximum recursion depth exceeded
Any idea what's going on in both tries? And how can I intercept method
calls without defining all of list's methods.
Thanks
More information about the Tutor
mailing list