The PyObject of python is the main object which is used in carrying out all python operations. It is used as a generic object for all transactions. Even though it might sound out of sync for the C programming language, we can call it the base object of python. Let us dwelve into the structure of it. The declaration is in the file Include/object.h

typedef struct _object {
} PyObject;

It contains the macro PyObject_HEAD which we shall understand.

#define PyObject_HEAD \
_PyObject_HEAD_EXTRA \
Py_ssize_t ob_refcnt; \
struct _typeobject *ob_type;

It contains three parts:

  1. The macro _PyObject_HEAD_EXTRA.
  2. The reference count of the object. This is the most important part.
  3. The type object, which we will look into detail in the coming post.

#define _PyObject_HEAD_EXTRA \
struct _object *_ob_next; \
struct _object *_ob_prev;

This is used to construct a doubly linked list of all objects in python.

We shall also look at useful macros of the PyObject.

#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s