[Tutor] __getattr__ and __setattr__ ???

karthik Guru karthikg@aztec.soft.net
Mon, 12 Nov 2001 10:58:26 +0530


that was more than helpful. Thanks a lot.
looked at some of the recipies in the python cookbook @active state's site.
But am finding some of the things difficult to comprehend as of now! :-(

karthik.



-----Original Message-----
From: Patrick K. O'Brien [mailto:pobrien@orbtech.com]
Sent: Saturday, November 10, 2001 8:30 PM
To: karthik Guru; tutor@python.org
Subject: RE: [Tutor] __getattr__ and __setattr__ ???


Yes and no. __setattr__ will always be called, __getattr__ is only called
when the attribute cannot be found in the objects local dictionary, which is
t.__dict__ in this case. In your example, the __getattr__ *is* getting
called because it also gets called from *within* the __init__. Since your
__setattr__ doesn't actually set the value of the attribute in the local
dictionary (self.__dict__[name] = value) it doesn't exist. So then print
t.name can't find it in the local objects dictionary and the __getattr__
gets invoked. Normally this wouldn't happen. Of course, you can play lots of
tricks like this if you *do* want __getattr__ to *always* get invoked. But
that is a bit advanced and I won't go into that here. Also, Python 2.2 has
some new mechanisms that make all of this even easier and more doable on a
finer level of granularity.

Here is a shell session to show you a bit of what is happening:

Welcome To PyCrust 0.7 - The Flakiest Python Shell
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Startup script executed: C:\Code\.pythonrc.py
>>> 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)
...
>>> t = test()
set attr called name employee
>>> t.name
get attr called name
>>> dir(t)
get attr called __members__
get attr called __methods__
[]
>>> t.__dict__
{}
>>>

---
Patrick K. O'Brien
Orbtech
"I am, therefore I think."

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
karthik Guru
Sent: Saturday, November 10, 2001 8:49 AM
To: tutor@python.org
Subject: [Tutor] __getattr__ and __setattr__ ???
Importance: High


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.




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor