471,305 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

__setattr__ and __getattr__ with derived classes

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
Jul 18 '05 #1
0 1485

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Holger Joukl | last post: by
1 post views Thread by limodou | last post: by
3 posts views Thread by Thomas Heller | last post: by
4 posts views Thread by Enrico | last post: by
2 posts views Thread by mwojc | last post: by
7 posts views Thread by =?UTF-8?Q?Alexandru_Mo=C8=99oi?= | last post: by
2 posts views Thread by Jan Schilleman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.