Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __setattr__ and __getattr__ with derived classes

Reply
Thread Tools

__setattr__ and __getattr__ with derived classes

 
 
Anand
Guest
Posts: n/a
 
      12-18-2003
class base:
def __setattr__(self,attr,key,*unexpected):
print "Base Class :",attr,key,unexpected,self.__dict__
self.__dict__[attr] = key
def __getattr__(self,attr,*unexpected):
print "Base Class :",attr,unexpected,self.__dict__
return self.__dict__[attr]

class derived(base):
def __setattr__(self,attr,key,*unexpected):
print "Derived Class :",attr,key,unexpected,self.__dict__
self.__dict__[attr] = key
## def __getattr__(self,attr,*unexpected):
## print "Derived Class :",attr,unexpected
## return self.__dict__[attr]


if __name__ == '__main__':
c = derived()
d = base()
print 'No Init yet'
def init(self):
print 'inside init'
self.test = None
self.test1 = 'string'
self.test2 = 2
print self.test
print self.test1
print self.test2
def init2(self):
print 'inside init 2'
self.test = None
self.test3 = 'string'
self.test4 = 2
print self.test
## print self.test1
## print self.test2
base.__dict__['__init__'] = init
c = derived()
d = base()
c.test
d.test
print "Base Had init"
derived.__dict__['__init__'] = init2
c = derived()
d = base()
print c.test1
print d.test1
================================================== =====
Output
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++
No Init yet
inside init
Derived Class : test None () {}
Derived Class : test1 string () {'test': None}
Derived Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
inside init
Base Class : test None () {}
Base Class : test1 string () {'test': None}
Base Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
Base Had init
inside init 2
Derived Class : test None () {}
Derived Class : test3 string () {'test': None}
Derived Class : test4 2 () {'test': None, 'test3': 'string'}
None
inside init
Base Class : test None () {}
Base Class : test1 string () {'test': None}
Base Class : test2 2 () {'test': None, 'test1': 'string'}
None
string
2
Base Class : test1 () {'test': None, 'test3': 'string', 'test4': 2}
================================================== ===================
look at the self.__dict__ in __init__ method
Base Class : test2 2 () {'test': None, 'test1': 'string'}

Now how did it change to
Base Class : test1 () {'test': None, 'test3': 'string', 'test4': 2}
in __getattr_ method?


I get a key error after this point. the dictionary contents in base
class were just fine in the __init__ method. but when i call the
__getattr__ python is giving me a different dictionary!!!

Can someone explain what is happening?
i am using python 2.3.3
 
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
derived classes and __getattr__ Alexandru Moșoi Python 7 09-05-2008 01:09 PM
__getattr__, __setattr__ and pickle Bruno Desthuilliers Python 6 08-13-2008 12:34 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
equivalent to python's __getattr__, __setattr__ aum Javascript 1 10-09-2006 03:59 AM
__getattr__, __setattr__ Thomas Heller Python 3 10-20-2005 04:50 PM



Advertisments