How to properly override the default factory of defaultdict?

Gregory Ewing greg.ewing at
Mon Feb 15 03:28:28 EST 2016

Herman wrote:
> I want to pass in the key to the default_factory of defaultdict and I found
> that defaultdict somehow can intercept my call to dict.__getitem__(self,
> key),

What's happening here is that defaultdict doesn't actually
override __getitem__ at all. Instead, it overrides __missing__,
which gets called by the standard dict's __getitem__ for a
missing key.

As Steven said, you don't need a defaultdict here at all,
just a dict subclass that defines __missing__ the way you


More information about the Python-list mailing list