assigning a custom mapping type to __dict__

Nick Coghlan ncoghlan at iinet.net.au
Wed Mar 2 13:24:56 CET 2005


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

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list