1. 程式人生 > >PyObject, PyTypeObject - Python 中的 '對象' 們

PyObject, PyTypeObject - Python 中的 '對象' 們

基於 asics reserve contain 字節 dealloc 類信息 cati cmp

  

1
PyObject, PyTypeObject - Python 中的 對象 2 3 一切皆對象 - 這是 Python 的學習和使用者們最最常聽到一句, 可謂 博大精深 - 勃大精深. 4 對象(Object) 是 Python 最核心的一個概念, 在 Python 中 一切皆是對象. 5 整數,字符串,類型type(整數類型, 字符串類型)統統都是對象. 6 7 Python 已經預先定義了一些類型對象, 如 int 類型, dict 類型, string 類型等等, 8 這些預先定義的類型對象被稱為
內建類型對象. 類型對象 實現了面向對象理論中的概念, 9 通過對類型對象的 實例化 創建實例對象, 如 int 對象, dict 對象, string 對象等. 10 實例對象可以被視為面向對象理論中對象這個概念在 python 中的體現. 11 另外, python 實現了通過 class A(object) 關鍵字實現自定義類型對象, 通過調用自定義的類型對象實例化類型對象 A(). 12 註: python 類型(type) -> (class) ; python 實例(instance) ->
對象(object) 13 14 怎樣理解 對象(object) 的概念 15 ‘對象’ 這一概念對人的思維是一個比較形象的概念, 然後計算機不能像人的思維一樣去理解對象這一概念. 16 計算機並不能理解 3 是一個整數, 而 3 是一個字符串, 對於它來說這些都是字節(byte). 17 通常, 對象 是數據以及基於這些數據的操作的集合. 對於計算機來說, 一個 對象 實際上就是一片被分配的內存空間, 18 這些內存可以是連續的, 也可以是離散的. 這片內存在更高的層面上被作為一個
整體來考慮和處理, 這個整體就是一個對象(對計算機來說) 19 而在這片內存中存儲著一系列的數據以及可以對這些數據進行操作(修改,讀取,運行等)的代碼(字節碼). 20 21 如大家所知道的那樣, Python 是有 C 實現的, 那麽對象 在 C 層面是什麽樣子呢? 22 PyObject 對象 23 Python 中, 對象 就是為 C 中的結構體(structure)在棧堆上申請的一塊兒內存區域. 24 object.h (Python 3.6) 25 typedef struct _object { 26 _PyObject_HEAD_EXTRA 27 Py_ssize_t ob_refcnt; # ob_refcnt 引用計數(垃圾回收機制) 28 struct _typeobject *ob_type; # ob_type 是一個指向 _typeobject 結構體的指針 29 } PyObject; 30 在 C 中定義 PyObject 結構體中 ob_refcnt 與 Python 內存管理機制有關, 它實現了基於 引用計數垃圾回收機制‘. 31 對於某一特定對象 A, 當有另一個 PyObject 引用 了該對象的時候, A 的引用計數( ob_refcnt ) 增加; 32 相反地, 如果 當這個 PyObject 被銷毀的時候, 對象 A 的引用計數( ob_refcnt ) 減少. 33 當對象 A 的引用計數( ob_refcnt ) 減少到 0 的時候, Python 的垃圾回收機制將把對象 A 從棧堆中刪除, 以便釋放內存供其他對象使用. 34引用計數( ob_refcnt ) 之外, ob_type 是一個指向 _typeobject 結構體的指針. 35 這個結構體對應著 Python 內部的一種特殊對象, 用來指定一個對象 類型的 類型對象, 如 對象 5 的類型對象 是 int 類型對象. 36 結論, 在 Python 中, 對象機制的核心, 一是 引用計數( ob_refcnt ), 另一個是 類型信息(ob_type). 37 通過類型信息(ob_type) 進一步對應到 一個對象的內容,大小 等信息. 38 39 PyTypeObject 類型信息 40 PyObject 中包括了所有對象 共有的信息的定義(ob_refcnt, ob_type). 41 在結構體 _typeobject 中, 主要定義了 4 類信息, 42 1, 類型名(tp_name), 主要供 Python 內部, 以及調試使用. 43 2, 創建該類型對象 時分配的內存空間相關信息, 即 tp_basicsize, tp_itemsize 44 3, 與該類型對象相關聯的操作信息, 如 tp_print; tp_getattr; tp_setattr 這些函數指針 45 4, 該類型對象的類型信息 (通過 PyType_Type 來確定一個對象的 類型對象) 46 47 object.h (Python 3.6) 48 typedef struct _typeobject { 49 PyObject_VAR_HEAD 50 const char *tp_name; /* For printing, in format "<module>.<name>" */ 51 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ 52 53 /* Methods to implement standard operations */ 54 destructor tp_dealloc; 55 printfunc tp_print; 56 getattrfunc tp_getattr; 57 setattrfunc tp_setattr; 58 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2) 59 or tp_reserved (Python 3) */ 60 reprfunc tp_repr; 61 62 /* Method suites for standard classes */ 63 PyNumberMethods *tp_as_number; 64 PySequenceMethods *tp_as_sequence; 65 PyMappingMethods *tp_as_mapping; 66 67 /* More standard operations (here for binary compatibility) */ 68 hashfunc tp_hash; 69 ternaryfunc tp_call; 70 reprfunc tp_str; 71 getattrofunc tp_getattro; 72 setattrofunc tp_setattro; 73 74 /* Functions to access object as input/output buffer */ 75 PyBufferProcs *tp_as_buffer; 76 77 /* Flags to define presence of optional/expanded features */ 78 unsigned long tp_flags; 79 80 const char *tp_doc; /* Documentation string */ 81 82 /* Assigned meaning in release 2.0 */ 83 /* call function for all accessible objects */ 84 traverseproc tp_traverse; 85 86 /* delete references to contained objects */ 87 inquiry tp_clear; 88 89 /* Assigned meaning in release 2.1 */ 90 /* rich comparisons */ 91 richcmpfunc tp_richcompare; 92 93 /* weak reference enabler */ 94 Py_ssize_t tp_weaklistoffset; 95 96 /* Iterators */ 97 getiterfunc tp_iter; 98 iternextfunc tp_iternext; 99 100 /* Attribute descriptor and subclassing stuff */ 101 struct PyMethodDef *tp_methods; 102 struct PyMemberDef *tp_members; 103 struct PyGetSetDef *tp_getset; 104 struct _typeobject *tp_base; 105 PyObject *tp_dict; 106 descrgetfunc tp_descr_get; 107 descrsetfunc tp_descr_set; 108 Py_ssize_t tp_dictoffset; 109 initproc tp_init; 110 allocfunc tp_alloc; 111 newfunc tp_new; 112 freefunc tp_free; /* Low-level free-memory routine */ 113 inquiry tp_is_gc; /* For PyObject_IS_GC */ 114 PyObject *tp_bases; 115 PyObject *tp_mro; /* method resolution order */ 116 PyObject *tp_cache; 117 PyObject *tp_subclasses; 118 PyObject *tp_weaklist; 119 destructor tp_del; 120 121 /* Type attribute cache version tag. Added in version 2.6 */ 122 unsigned int tp_version_tag; 123 124 destructor tp_finalize; 125 126 #ifdef COUNT_ALLOCS 127 /* these must be last and never explicitly initialized */ 128 Py_ssize_t tp_allocs; 129 Py_ssize_t tp_frees; 130 Py_ssize_t tp_maxalloc; 131 struct _typeobject *tp_prev; 132 struct _typeobject *tp_next; 133 #endif 134 } PyTypeObject;

PyObject, PyTypeObject - Python 中的 '對象' 們