[Python-bugs-list] [ python-Bugs-824854 ] dict.__init__ doesn't call subclass's __setitem__.

SourceForge.net noreply at sourceforge.net
Thu Oct 16 10:41:38 EDT 2003


Bugs item #824854, was opened at 2003-10-16 10:27
Message generated for change (Comment added) made by jemfinch
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=824854&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Jeremy Fincher (jemfinch)
Assigned to: Nobody/Anonymous (nobody)
Summary: dict.__init__ doesn't call subclass's __setitem__.

Initial Comment:
The easiest way to describe this is with the code I was
using that uncovered this bug:

class TwoWayDictionary(dict):
    __slots__ = ()
    def __setitem__(self, key, value):
        dict.__setitem__(self, key, value)
        dict.__setitem__(self, value, key)

I suspected this bug might happen, so I wrote this test:

class TwoWayDictionaryTestCase(unittest.TestCase):
    def testInit(self):
        d = TwoWayDictionary(foo='bar')
        self.failUnless('foo' in d)
        self.failUnless('bar' in d)

Basically, dict.__init__ is calling dict.__setitem__
even in subclasses.

----------------------------------------------------------------------

>Comment By: Jeremy Fincher (jemfinch)
Date: 2003-10-16 10:41

Message:
Logged In: YES 
user_id=99508

If someone went to the trouble to be able to define what
"kermel methods" are used in the dict/list/whatever else
implementations, would it even be accepted, or would it be
considered too useless/slow/etc. to be bothered with?

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2003-10-16 10:33

Message:
Logged In: YES 
user_id=6656

.update() won't call TwoWayDictionary.__setitem__ either. 
Ditto .setdefault.

You'll see similar behaviour for lists too.

AFAIK, nowhere do the Python docs promise that the
dictionary implementation is defined in terms of certain
"primitive methods".  You may think this a bug, but it's a
whole lotta work to "fix" it.

FWIW, you might be happier using UserDict.DictMixin.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=824854&group_id=5470



More information about the Python-bugs-list mailing list