Counting method calls

  • Thread starter =?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
  • Start date
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Hi, I know I'm being dumb but, why does it not work?
.... 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
 
C

chris.monsanto

Hi, I know I'm being dumb but, why does it not work?

... 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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top