浅谈 Python 的对象实现(PyObject)
PyObject Everything is object, except keywords 所有的都是对象,除了关键字 Include/object.h /* PyObject_HEAD 是所有 OBJECT 的初始化段 */ #define PyObject_HEAD PyObject ob_base; typedef struct _object PyObject; struct _object { Py_ssize_t ob_refcnt; PyTypeObject *ob_type; }; /* Py_GIL_DISABLED (无 GIL 锁的版本),添加了额外的 mutex */ struct _object { uintptr_t ob_tid; uint16_t _padding; struct _PyMutex ob_mutex; // per-object lock uint8_t ob_gc_bits; // gc-related state uint32_t ob_ref_local; // local reference count Py_ssize_t ob_ref_shared; // shared (atomic) reference count PyTypeObject *ob_type; }; ...