Inheriting from OrderedDict causes problem
Bruce Eckel
lists.eckel at gmail.com
Wed Feb 22 11:53:52 EST 2012
Notice that both classes are identical, except that one inherits from
dict (and works) and the other inherits from OrderedDict and fails.
Has anyone seen this before? Thanks.
import collections
class Y(dict):
def __init__(self, stuff):
for k, v in stuff:
self[k] = v
# This works:
print Y([('a', 'b'), ('c', 'd')])
class X(collections.OrderedDict):
def __init__(self, stuff):
for k, v in stuff:
self[k] = v
# This doesn't:
print X([('a', 'b'), ('c', 'd')])
""" Output:
{'a': 'b', 'c': 'd'}
Traceback (most recent call last):
File "OrderedDictInheritance.py", line 17, in <module>
print X([('a', 'b'), ('c', 'd')])
File "OrderedDictInheritance.py", line 14, in __init__
self[k] = v
File "C:\Python27\lib\collections.py", line 58, in __setitem__
root = self.__root
AttributeError: 'X' object has no attribute '_OrderedDict__root'
"""
More information about the Python-list
mailing list