Catching all methods before they execute

jamesthiele.usenet at jamesthiele.usenet at
Mon Mar 7 20:08:02 CET 2005

>jamesthiele.use... at wrote:
>> I have run into some cases where I would like to run a class method
>> anytime any class method is invoked.
>Perhaps you want __getattribute__ on a new-style class?
>Michael Hoffman

Perhaps I do. The docs say that __getattribute__ is called on all
attribute references, so I tried to make an undoable list as follows:
% cat
class Undoable(object):
    def __init__(self, superclass):
        self.superclass = superclass
        print "__init__"

    def __getattribute__(self, name):
        print "__getattribute__"
        self.superclass.__getattribute__(self, name)

    def SaveState(self):
        print "SaveState"

    def RestoreState(self):

l = Undoable(list)
l = [1, 2, 3]
print l.count(1)
% python

It appears that __init__ in Undoable is called, count() in list is
called, but not __getattribute__ or SaveState in Undoable.

What don't I understand?

More information about the Python-list mailing list