User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,540 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,275 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1352 | Replies: 1 | Solved
Reply
Join Date: Sep 2007
Posts: 4
Reputation: thompsongunner is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
thompsongunner thompsongunner is offline Offline
Newbie Poster

Help Inheritance from other objects

  #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:
<class '__main__.ItemTable'>
OrderedDict([('ITEM1',[(1,8),111]),('ITEM2',[(1,8),222]),('ITEM3',[(1,8),333])] )
[(1,8),222]
['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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Inheritance from other objects

  #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
  26.  
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 4:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC