sublcassing dict without losing functionality

Mathias Waack M.Waack at gmx.de
Mon Nov 1 21:52:52 CET 2004


Steven Bethard wrote:

> I'd like to subclass dict to disallow overwriting of keys,
> something like:

<snip>

> The problem is, dict doesn't appear to call __setitem__ in any of
> the __init__ forms, so none of the following raise errors as I'd
> like them to:

<snap>

> etc.  Is there a simple way to override this behavior in dict
> without
> having to rewrite __init__?  There are so many cases in
> dict.__init__ that I'm hesitant to try to reproduce them all...

How about starting with UserDict (source comes with your python
distribution) and modifying it for your needs?

Mathias



More information about the Python-list mailing list