Subclassing Python's dict

Carl Banks pavlovevidence at gmail.com
Thu Aug 6 06:38:53 EDT 2009


On Aug 5, 7:39 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> Sergey Simonenko a écrit :
>
> > Hi,
>
> > I subclass builtin 'dict' in my application and experience some problems
> > with it.
>
> > The whole issue is that I should redefine 'setdefault' and 'update'
> > methods after redefining '__setitem__' or/and '__delitem__',
> > otherwise 'update' and 'setdefault' ignore redefined '__setitem__' and
> > use builtin dict's one so dict looks kinda like a black box.
>
> > Another guy have reported me that he experiences similar problems with
> > subclassing builtin 'list'.
>
> I indeed notice this behaviour here (Python 2.6.2). I'm afraid it has to
> do with some optimization tricks (dict being the very fundamental data
> structure in Python, it has to be higly optimized).

You can ultimately blame it on optimization in this case, but in
general there isn't any way (in Python) to require that __setitem__ be
the common point for all modifications.

In fact this is an example of a general condition of OOP.  A "high-
level" method may or may not call a "low-level" method to do its dirty
work, and there is no way to know this simply from the class's
interface.  You need to have knowledge of the implementation to know
whether you need to override "high-level" or "low-level" methods.  In
general you can't safely subclass based on the interface alone; you
have to know what you're subclassing.

(As an aside: this intuitively is the reason why I never cared much
for some of the arguments against inheritance.  Many arguments against
inheritance go like this: "Inheritance is bad because X can happen if
you're not careful".  Doesn't mean anything to me because you have be
careful anyway.)

Because of the care required when subclassing, I tend to use
inheritance only when the intended base class is A. also under my
control, or B. specifically designed to be subclassed.  Examples of B
in the standard Python library are Queue and Thread.  list and dict
are allowed to be subclassed but aren't specifically designed for it,
so I tend not to subclass those.  (It is occasionally indispensible,
though.)


Carl Banks



More information about the Python-list mailing list