Subclassing dictionary in Python.

Oct 12, 2016 15:34 · 68 words · 1 minute read python

Subclassing dictionary in Python.

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?

tweet Share