assigning a custom mapping type to __dict__

Steven Bethard steven.bethard at gmail.com
Tue Mar 1 12:31:33 EST 2005


Daniel Cer wrote:
>>>Why not just inherit from dict? That seems to work.
>>
>>Because that isn't the question - Steven knows how to make it work, what he's
>>curious about is why things are the way they are :)
> 
> Sorry, didn't mean to be a pest :)
> 
> I guess I assumed Steve already knew that he could inherit from dict.
> That being said, I was wondering why pragmatically this wouldn't be the
> right thing to do (in order to do what he seemed to want to do).

The problem with inheriting from dict is that you then need to override 
*all* the methods in the dict object, because they all go straight to 
Python's dict'c C code functions.  So just because you redefine 
__getitem__ doesn't mean you don't still have to redefine __contains__, 
get, update, etc.  UserDict.DictMixin can help with this some, but the 
ideal situation would be to only have to define the methods you actually 
support.  Inheriting from dict likely means you have to redefine a bunch 
of functions to raise Exceptions saying that they're unsupported.

STeVe



More information about the Python-list mailing list