471,318 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Counting method calls

Hi, I know I'm being dumb but, why does it not work?
>>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
TIA
Sep 21 '07 #1
1 1292
On Sep 21, 7:15 pm, Ricardo Aráoz <ricar...@gmail.comwrote:
Hi, I know I'm being dumb but, why does it not work?
>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

TIA
__getattr__ only works for attributes that don't exist. The name is
sorta confusing...

Sep 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Elbert Lev | last post: by
14 posts views Thread by Mike N. | last post: by
7 posts views Thread by sathyashrayan | last post: by
10 posts views Thread by cj | last post: by
1 post views Thread by oec.deepak | last post: by
5 posts views Thread by thorsten.schilling | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.