What is the meaning of Py_INCREF a static PyTypeObject?

Xiang Zhang 18518281186 at 126.com
Thu Nov 12 03:05:09 EST 2015


Recently I am learning Python C API.

When I read the tutorial 
<https://docs.python.org/3/extending/newtypes.html#the-basics>, defining 
new types, I feel confused. After PyType_Ready(&noddy_NoddyType) comes 
Py_INCREF(&noddy_NoddyType). Actually noddy_NoddyType is a static struct 
so I don't understand why I need to Py_INCREF it. Since it's 
Py_INCREFed, does it mean sometimes we also need to Py_DECREF it? But 
then it seems that type_dealloc will be invoked and it will fail 
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);

I haven't read the typeobject.c source code totally so I think I must 
have missed something. But I am quite confused. Hope someone can give me 
some hints. Thanks in advance.



More information about the Python-list mailing list