Overriding dict constructor

Hrvoje Niksic hniksic at xemacs.org
Mon Sep 20 16:15:22 CEST 2010


Christian Heimes <lists at cheimes.de> writes:

> Am 20.09.2010 13:11, schrieb Steven D'Aprano:
>> I have a dict subclass that associates extra data with each value of the 
>> key/value items:
> [...]
>> How can I fix this?
>
> Since the dict class is crucial to the overall performance of Python,
> the dict class behaves bit different than other classes. I don't know if
> this is documented somewhere. Dict methods call the PyDict_GetItem
> function directly instead of going through the type's struct.

Normally where this kind of optimization is necessary Python is careful
to use PyFoo_CheckExact to find out if it is dealing with an instance of
the class or a subclass, and only goes through the fast path for the
former case.  That PyDict_Merge (called by dict_init) doesn't do this
could be considered a bug because it constrains dict subclass in a way
that is hard to work around, and without a clear gain in performance
compared to using an exact check.

dict_fromkeys is an example in the same file that uses
PyDict_CheckExact.



More information about the Python-list mailing list