Subclassing Python's dict

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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).
 
S

Sergey Simonenko

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'.

Kind regards, Sergey.
 
C

Carl Banks

Sergey Simonenko a écrit :





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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top