[Tutor] __getattr__ and __setattr__ ???

karthik Guru karthikg@aztec.soft.net
Sat, 10 Nov 2001 20:19:01 +0530


hi all,

class test:
    def __init__(self):
            self.name="employee"
    def __getattr__(self,name):
            print 'get attr called ' + str(name)
    def __setattr__(self,name,value):
            print 'set attr called ' + str(name) + " " + str(value)

if __name__ == '__main__':
    t = test()
    print t.name
    t.name = "manager"


This is the code i have.
My understanding was that __getattr__() or __setattr__() w'd'nt be called
when
the main executes as it can find the attribute in the local namespace.

BUT it does call the 2 methods.

This means i can trap the events when someone is "reading" or "modifying" my
instance attributes?
Somewhat similar to the way "properties" work in C#??

please clear my confusion.

regards
karthik.