I looked at the source code for OrderedDict and it has this:
def __setitem__(self, key, value,
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
'od.__setitem__(i, y) <==> od[i]=y'
# Setting a new item creates a new link at the end of the linked list,
# and the inherited dictionary is updated with the new key/value pair.
if key not in self:
self.__map[key] = link = Link()
root = self.__root
last = root.prev
link.prev, link.next, link.key = last, root, key
last.next = link
root.prev = proxy(link)
dict_setitem(self, key, value)
I want to have a look at the __map object inside the OrderedDict but when I run dir() on an OrderedDict object there is no __map
object in the output. In fact the __dict__
attribute is empty:
>>> from collections import OrderedDict
>>> a = OrderedDict()
>>> a[1] = 1
>>> a
OrderedDict([(1, 1)])
>>> dir(a)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'move_to_end', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> a.__dict__
{}
>>> a.__map
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'collections.OrderedDict' object has no attribute '__map'
So, 2 questions really.
Why is the
__map
attribute (and__root
and other attributes) not showing up in dir()What do I have to do to access the __map attribute?
EDIT: I tried the name mangling thing as well:
>>> a._OrderedDict__map
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'collections.OrderedDict' object has no attribute '_OrderedDict__map'