•
•
•
•
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
![]() |
•
•
Join Date: Sep 2007
Posts: 4
Reputation:
Rep Power: 0
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
<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: 562
Reputation:
Rep Power: 4
Solved Threads: 72
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- 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


Linear Mode