[Pyrex] Extending collections.defaultdict

Stefan Behnel stefan_ml at behnel.de
Thu Apr 2 13:29:33 CEST 2009


Robert Bradshaw wrote:
> On Apr 2, 2009, at 4:17 AM, Franck Pommereau wrote:
>
>> I would like to create in Cython/Pyrex a new type by extending
>> collections.defaultdict class. I've found that I can extend dict by
>> using :
>>
>> cdef extern from "dictobject.h":
>>     ctypedef class __builtin__.dict [object PyDictObject]:
>>         pass
>>
>> cdef class hdict (dict):
>>     ...
>>
>> But there is no header file such as "dictobject.h" for module
>> collections nor I could find one that declares defaultdict.
>>
>> Does anyone knows how I could declare the existence of defaultdict?
>
> I PyDictObject is #included into the standard Python.h file, so you
> could probably just get it from there. You need to make sure you have
> the development version of Python installed.

Actually, "defaultdict" is a subtype of dict that adds an additional
field. The problem is that "defaultdict" is not a public type, it is only
defined in "Modules/_collections.c" in the Python sources.

You may manage to redefine it in your own header file and use that, but
that will not safe you from future changes to the type inside Python. It's
not a public type, so the Python developers are free to change it at need.

Stefan




More information about the Pyrex mailing list