Welcome to My Daily Blog.
- Record Daily Thoughts.
Welcome to My Daily Blog.
PyObject Everything is object, except keywords 所有的都是对象,除了关键字 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /* FILE: cpython/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; }; ob_refcnt 即引用计数器,ob_type 是类型对象的指针 这里的 ob_type 指向了一个描述了该如何操作此类型的类型对象 HOW: [this->data] 使用 [this->ob_type] 的 处理函数, 而 处理函数 则是 this->ob_type->data 里的数据,因而可以随意在运行时改变类型的 属性(处理函数),实则是改变上一层的对象的数据。 ...