assigning a custom mapping type to __dict__

Steven Bethard steven.bethard at gmail.com
Wed Mar 2 09:18:42 EST 2005


Nick Coghlan wrote:
> Steven Bethard wrote:
> 
>> 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.
> 
> 
> You're just lucky the affected class is already overriding 
> __getattribute__, so the __dict__ is generally getting accessed from 
> Python code :)
> 
> If it weren't for that, object.c's direct calls to the PyDict_* API 
> would be making things even more fun for you than they already are (as 
> Duncan pointed out).

Yup, I noticed that.  Lucky us. =)

STeVe



More information about the Python-list mailing list