943,766 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4440
  • Python RSS
Oct 12th, 2007
0

Inheritance from other objects

Expand Post »
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.
python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thompsongunner is offline Offline
4 posts
since Sep 2007
Oct 12th, 2007
0

Re: Inheritance from other objects

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.

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: drawing in python.
Next Thread in Python Forum Timeline: Acronym code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC