[Python-ideas] Support other dict types for type.__dict__

Simon Sapin simon.sapin at kozea.fr
Sun Feb 26 09:26:50 CET 2012

Le 24/02/2012 00:34, Victor Stinner a écrit :
> I'm trying to create read-only objects using a "frozendict" class.
> frozendict is a read-only dict. I would like to use frozendict for the
> class dict using a metaclass, but type.__new__() expects a dict and
> creates a copy of the input dict.
> I would be nice to support custom dict type: OrderedDict and
> frozendict for example. It looks possible to patch CPython to
> implement this feature, but first I would like first to know your
> opinion about this idea:-)


Combining ideas from other messages in this thread: would this work?

1. Inherit from frozendict
2. Define a __getattr__ that defers to frozendict.__getitem__
3. Use an empty __slots__ so that there is no "normal" instance attribute.

Thinking about it a bit more, it’s probably the same as having a normal 
__dict__ and raising in __setattr__ and __delattr__. Isn’t this how you 
implement frozendict? (Raise in __setitem__, __delitem__, update, etc.)

Simon Sapin

More information about the Python-ideas mailing list