| | |
Inheritance from other objects
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Solved Threads: 0
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
I expect the print statement output to be something like:
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
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)
from odict import * class ItemTableA(OrderedDict): def __init__(self): self = OrderedDict() self.update( [ ('ITEM1', [(1, 8), 111]) ] ) self.update( [ ('ITEM2', [(1, 8), 222]) ] ) self.update( [ ('ITEM3', [(1, 8), 333]) ] ) class ItemTableB(OrderedDict): def __init__(self): self = OrderedDict() self.update( [ ('ITEM1', [(1, 16), 111]) ] ) self.update( [ ('ITEM2', [(1, 16), 222]) ] ) self.update( [ ('ITEM3', [(1, 32), 333]) ] ) if __name__ == '__main__': myTable = ItemTableA() print type(myTable) # P1 print myTable # P2 print myTable['ITEM2'] # P3 print myTable.keys() # P4
Python Syntax (Toggle Plain Text)
<class '__main__.ItemTable'> OrderedDict([('ITEM1',[(1,8),111]),('ITEM2',[(1,8),222]),('ITEM3',[(1,8),333])] ) [(1,8),222] ['ITEM1', 'ITEM2', 'ITEM3']
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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.

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)
class Parent(object): def __init__(self): self.a = "A" self.b = "B" def __str__(self): return self.a + self.b class BrokenChild(Parent): def __init__(self): self = Parent() class WellChild(Parent): def __init__(self): super(WellChild, self).__init__() # OR, # Parent.__init__() a = Parent() #b = BrokenChild() c = WellChild() print a #print b print c
![]() |
Similar Threads
- modifying the same member of many objects (C++)
- problem with headers and streams (C++)
- Passing arrays of objects to functions (C++)
- Inheritance problems (Java)
- Passing Class Objects, Instances (C++)
- Creating and destroying objects (C++)
- Array limit (C)
- New To Programming = ME!! (C++)
Other Threads in the Python Forum
- Previous Thread: drawing in python.
- Next Thread: Acronym code
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib





