Inheritance from other objects

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 4
Reputation: thompsongunner is an unknown quantity at this point 
Solved Threads: 0
thompsongunner thompsongunner is offline Offline
Newbie Poster

Inheritance from other objects

 
0
  #1
Oct 12th, 2007
I'm trying to get a grasp of inheritance in Python but am having problems.

I am trying to create a derived class but want the derived class to only provide an initialized instance of the base class. Specifically I would like to inherit from OrderedDict as shown in the following example.
  1.  
  2. from odict import *
  3.  
  4. class ItemTableA(OrderedDict):
  5. def __init__(self):
  6. self = OrderedDict()
  7. self.update( [ ('ITEM1', [(1, 8), 111]) ] )
  8. self.update( [ ('ITEM2', [(1, 8), 222]) ] )
  9. self.update( [ ('ITEM3', [(1, 8), 333]) ] )
  10.  
  11. class ItemTableB(OrderedDict):
  12. def __init__(self):
  13. self = OrderedDict()
  14. self.update( [ ('ITEM1', [(1, 16), 111]) ] )
  15. self.update( [ ('ITEM2', [(1, 16), 222]) ] )
  16. self.update( [ ('ITEM3', [(1, 32), 333]) ] )
  17.  
  18. if __name__ == '__main__':
  19. myTable = ItemTableA()
  20. print type(myTable) # P1
  21. print myTable # P2
  22. print myTable['ITEM2'] # P3
  23. print myTable.keys() # P4
I expect the print statement output to be something like:
  1. <class '__main__.ItemTable'>
  2. OrderedDict([('ITEM1',[(1,8),111]),('ITEM2',[(1,8),222]),('ITEM3',[(1,8),333])] )
  3. [(1,8),222]
  4. ['ITEM1', 'ITEM2', 'ITEM3']
Instead, when I run this program the last 3 print statements fail. Please cut-n-paste-run to see exceptions.

I suspect it's an operator error in the __init__() statement.
Any ideas? Does my attempt to create a bunch of initialized tables make sense? (perhaps there's a better way)

Thanks,
tg
Last edited by thompsongunner; Oct 12th, 2007 at 4:36 pm. Reason: cleanup
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Inheritance from other objects

 
0
  #2
Oct 12th, 2007
Well, I don't have an 'odict' module, so my error message was on line 1.

Below is an example of how to do inheritance correctly. If you uncomment lines 21 and 24, you should get an error message similar to the one you were getting.

Basically, the problem with your code is that in __init__, you were clobbering self with a new OrderedDict() object. But then when __init__ returns, its namespace disappears (i.e., all variables are garbage collected), and the change you made is entirely lost.

By contrast, in the WellChild() object below, WellChild's __init__ calls the parent's __init__() -- this should (almost) ALWAYS be done by children. The Parent.__init__ then sets up all of the inherited data members.

The super(type, instance) function looks up the parent of WellChild. Or, you can do it manually as I indicated in the comment. [Note: the documentation on super() is wrong . It shows super(type), but an instance is also required].

Hope that helps.

  1. class Parent(object):
  2. def __init__(self):
  3. self.a = "A"
  4. self.b = "B"
  5.  
  6. def __str__(self):
  7. return self.a + self.b
  8.  
  9. class BrokenChild(Parent):
  10. def __init__(self):
  11. self = Parent()
  12.  
  13. class WellChild(Parent):
  14. def __init__(self):
  15. super(WellChild, self).__init__()
  16. # OR,
  17. # Parent.__init__()
  18.  
  19.  
  20. a = Parent()
  21. #b = BrokenChild()
  22. c = WellChild()
  23. print a
  24. #print b
  25. print c
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC