A
Anand
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
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