Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Counting method calls

Reply
Thread Tools

Counting method calls

 
 
=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=
Guest
Posts: n/a
 
      09-21-2007
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
 
Reply With Quote
 
 
 
 
chris.monsanto@gmail.com
Guest
Posts: n/a
 
      09-22-2007
On Sep 21, 7:15 pm, Ricardo Aráoz <ricar...@gmail.com> wrote:
> 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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting function calls thorsten.schilling@gmail.com C++ 5 01-01-2008 06:57 PM
ods calls business object then method calls ta with output params andy6 ASP .Net 2 06-09-2006 01:54 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
MoVoIP - FREE MOBILE Inetrnet Phone Calls - FREE Internet Phone Calls ubifone VOIP 0 07-29-2005 04:31 PM
Sequence of constructor calls and destructor calls. Honne Gowda A C++ 2 10-31-2003 09:31 AM



Advertisments