Subclassing dictionary in Python.

Posted on

Take a look at this:

>>> c={'something': 2}
>>> c.__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute '__dict__'

Now let’s subclass dict:

>>> class B(dict):
...     def pass
>>> b=B(something=2)
>>> b
{'something': 2}
>>> b.__dict__

So dictionary don’t have __dict__ but if you subclass them, then they have __dict__. Interesting, eh?