Python Internals: PyObject
Sergei Danielian (via Hacker News):
Each Python’s type implementation (
PyIntObject
,PyFloatObject
orPyDictObject
) hasPyObject_HEAD
located as its first member (or the first member of its first member, and so on). This member sub-object is guaranteed to be located at the same address as the full object.The
PyObject_HEAD
refers at that member sub-object, but could be cast to the full type onceob_type
has been inspected to get knowledge of what the full type is.
Here are nine lectures walking through the internals of CPython, the canonical Python interpreter implemented in C. They were from a dynamic programming languages course that I taught in Fall 2014 at the University of Rochester. The format isn’t ideal, but I haven’t seen this level of detail about CPython presented online, so I wanted to share these videos.
We saw above the extra type info layer when moving from a C integer to a Python integer. Now imagine you have many such integers and want to do some sort of batch operation on them. In Python you might use the standard
List
object, while in C you would likely use some sort of buffer-based array.A NumPy array in its simplest form is a Python object build around a C array. That is, it has a pointer to a contiguous data buffer of values. A Python list, on the other hand, has a pointer to a contiguous buffer of pointers, each of which points to a Python object which in turn has references to its data (in this case, integers).