3 ms·
One thing that was really educational for me was reading how CPython implemented their object system. https://github.com/python/cpython/blob/master/Include/obje
by wybiral 9y ago
One thing that was really educational for me was reading how CPython implemented their object system. https://github.com/python/cpython/blob/master/Include/object.h https://github.com/python/cpython/blob/master/Include/object...
Basically objects are structs that all share the same header which contains a pointer to the object type (ob_type) and the reference count (ob_refcnt) for memory management.
That way you can always cast back to a pointer of that header struct and access ob_type and ob_refcnt no matter what the actual implemented struct contains afterwards.
ob_type contains the name of the type and function pointers for common methods used by objects (getattr, setattr, hash, arithmetic, etc).