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.
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
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