0

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.

  1. Why is the __map attribute (and __root and other attributes) not showing up in dir()

  2. 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'
johnny
  • 19
  • 2
  • 2
    Possible duplicate of [Python name mangling](https://stackoverflow.com/questions/7456807/python-name-mangling) – Azat Ibrakov Dec 19 '18 at 12:23
  • 1
    you really don't need to (and should not) access private fields – Azat Ibrakov Dec 19 '18 at 12:24
  • 1
    I just want to understand how OrderedDict works internally. – johnny Dec 19 '18 at 12:28
  • 2
    I think the reason trying to access `._OrderedDict__map` fails is because this Python implementation of OrderedDict is not actually used. Instead, a [C implementation](https://github.com/python/cpython/blob/master/Objects/odictobject.c) is. – L3viathan Dec 19 '18 at 12:32
  • 1
    @L3viathan yes exactly https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L303 trying to think of a way of actually importing the python implementation without modifying the source ode? – Chris_Rands Dec 19 '18 at 12:37

0 Answers0