[Python-3000] A better way to initialize PyTypeObject

Talin talin at acm.org
Tue Nov 28 11:53:51 CET 2006


Greg Ewing wrote:
> Talin wrote:
> 
>> What you end up with is code that looks like this:
>>
>> PyTypeObject myType = {
>>      PyObject_HEAD_INIT(NULL)
>>      0,
>>      "myType",
>>      sizeof(myInstance)
>> }
>>
>> void init() {
>>      if (PyType_ReadyInit( &myType, myTypeMethods, myTypeData ) < 0)
>>     return;
>> }
> 
> If you're going that far, why not go a step further and do
> away with the statically-declared type object altogether?
> 
>   PyTypeObject *myType;
> 
>   myType = PyType_Create(sizeof(myInstance), myTypeMethods, myTypeData);

That makes sense - I was trying to avoid allocations (my day-job habits 
leaking through again), but this is Python after all, and avoiding a 
single memory allocation per type is silly.

> -- 
> Greg
> 


More information about the Python-3000 mailing list