Catching all methods before they execute

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


>jamesthiele.use... at gmail.com 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 Undoable.py
class Undoable(object):
    def __init__(self, superclass):
        self.superclass = superclass
        print "__init__"

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

    def SaveState(self):
        print "SaveState"

    def RestoreState(self):
        pass

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

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