[capi-sig] Python 3.x PyObject_HEAD versus PyObject_VAR_HEAD

M.-A. Lemburg mal at egenix.com
Tue Feb 2 12:54:06 CET 2010

Philip Semanchuk wrote:
> On Feb 1, 2010, at 5:27 AM, M.-A. Lemburg wrote:
>> Perhaps you are confusing this with PyVarObject_HEAD_INIT() ?!
> THanks for your reply. You're right, I should have said that it is
> PyVarObject_HEAD_INIT that's used hundreds of times while
> PyObject_HEAD_INIT is hardly used at all (according to `grep -r`).
> Actually, I can state that even more strongly: PyObject_HEAD_INIT is
> only used once -- in the #define for PyVarObject_HEAD_INIT.
> After spending another hour reading the Python source, I think I get it
> now. As is often the case, the problem came from not reading carefully.
> When I saw this:
>    PyTypeObject PyFloat_Type = {
>        PyVarObject_HEAD_INIT(&PyType_Type, 0)
>        "float",
>        sizeof(PyFloatObject),

Right, it's being used to init the (static) type object PyFloat_Type
in the above case.

> I saw PyVarObject_HEAD_INIT being used to initialize a PyFloatObject,
> which is not the case -- it's initializing a PyTypeObject. Since the
> PyTypeObject struct definition uses PyObject_VAR_HEAD, it makes sense
> that PyVarObject_HEAD_INIT is used to initialize it.
> That also explains why PyVarObject_HEAD_INIT shows up so much in the
> Python source -- it's all those PyTypeObjects getting initialized.


> This brings up two more questions that I could probably answer with more
> reading, but if you have time for some further explanation I'd
> appreciate it. First, in what context would one use PyObject_HEAD_INIT?
> Second, why does the PyTypeObject struct definition use
> PyObject_VAR_HEAD when a type object has no concept of length?

It does: additional members can be stored in the variable part of the
object, ie. low-level attributes which are exposed as slots in

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Feb 02 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the capi-sig mailing list