__setattr__ and __getattr__ with derived classes

Discussion in 'Python' started by Anand, Dec 18, 2003.

  1. Anand

    Anand Guest

    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
    Anand, Dec 18, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Heller

    __getattr__, __setattr__

    Thomas Heller, Oct 20, 2005, in forum: Python
    Replies:
    3
    Views:
    670
    Thomas Heller
    Oct 20, 2005
  2. Replies:
    1
    Views:
    387
    myork
    May 23, 2007
  3. Bruno Desthuilliers

    __getattr__, __setattr__ and pickle

    Bruno Desthuilliers, Aug 12, 2008, in forum: Python
    Replies:
    6
    Views:
    364
    Bruno Desthuilliers
    Aug 13, 2008
  4. Alexandru Moșoi

    derived classes and __getattr__

    Alexandru Moșoi, Sep 5, 2008, in forum: Python
    Replies:
    7
    Views:
    281
    Bruno Desthuilliers
    Sep 5, 2008
  5. aum
    Replies:
    1
    Views:
    125
Loading...

Share This Page