Go deep into the source code to parse objects and types in Python,
Object
How is object implemented in C language?
In Python, objects are divided into two types: Fixed Length (int) and non-fixed length (list/dict)
All objects have the same things. The source code is defined as PyObject and PyVarObject. Both definitions have a common header definition PyObject_HEAD (in fact, PyVarObject has its own header definition PyObject_VAR_HEAD, but it actually uses PyObject_HEAD ).
Source code location: Include/object. h
PyObject_HEAD
Inside Python, each object has the same header.
Definition
/* PyObject_HEAD defines the initial segment of every PyObject. */#define PyObject_HEAD \ _PyObject_HEAD_EXTRA \ Py_ssize_t ob_refcnt; \ struct _typeobject *ob_type;
Description
1. _ PyObject_HEAD_EXTRA
Ignore first, two-way linked list structure, garbage collection later
2. Py_ssize_t ob_refcnt
Py_ssize_t is determined during compilation, integer
Ob_refcnt, reference count, which is related to Python's memory management mechanism (garbage collection based on reference count)
3. struct _ typeobject * ob_type
* Ob_type refers to the pointer to a type object (pointing to the _ typeobject struct)
Determines the object type!
PyObject
Definition
typedef struct _object { PyObject_HEAD } PyObject;
Description
1. Dependency
PyObject-> PyObject_HEAD
Structure
PyVarObject
Definition
typedef struct { PyObject_VAR_HEAD} PyVarObject;#define PyObject_VAR_HEAD \ PyObject_HEAD \ Py_ssize_t ob_size; /* Number of items in variable part */
Description
1. Dependency
PyVarObject-> PyObject_VAR_HEAD-> PyObject_HEAD
2. Py_ssize_t ob_size
Ob_size: the number of elements that a variable-length object can contain.
Structure
Code relationship
Several Methods
Object-related methods
# Define Py_REFCNT (ob) (PyObject *) (ob)-> ob_refcnt)
Read reference count
# Define Py_TYPE (ob) (PyObject *) (ob)-> ob_type)
Get object type
# Define Py_SIZE (ob) (PyVarObject *) (ob)-> ob_size)
Number of read elements (len)
Methods related to reference count
Py_INCREF (op) increases the object reference count
Py_DECREF (op) reduces the object reference count. If the Count bit is 0, call _ Py_Dealloc
_ Py_Dealloc (op) calls the tp_dealloc method of the corresponding type (for different types of collection behaviors, various cache pool mechanisms will be shown later)
Others
Several parameters involved
Ob_refcnt reference count, related to memory management/garbage collection
Ob_type, involving Python type systems
Type
Example
>>> A = 1 >>> a1 >>> type (a) <type 'int' ># two equivalent >>> type ()) <type 'type' >>>> type (int) <type 'type' >#two equivalent >>> type ())) <type 'type' >>> type (int) <type 'type'>
We reverse derive how an int object is generated.
1. First, define a type called PyTypeObject
Code position: Include/object. h
Definition
Typedef struct _ typeobject {/* MARK: base. Note that it is a variable-length object */PyObject_VAR_HEAD const char * tp_name;/* For printing, in format "<module>. <name> "* // type name: Py_ssize_t tp_basicsize, tp_itemsize;/* For allocation * // the size of memory space allocated when an object of this type is created // a bunch of method definitions, functions and pointers/* Methods to implement standard operations */printfunc tp_print; hashfunc tp_hash;/* Method suites for standard classes */PyNumberMethods * tp_as_number; // numeric object operation limit * tp_as_sequence; // sequence object operation PyMappingMethods * tp_as_mapping; // dictionary object operation // a bunch of attribute definitions ....} pyTypeObject;
Description
1. PyObject_VAR_HEAD
Variable-length object
2. const char * tp_name
Tp_name, type name String Array
All types are "instances" of PyTypeObject: PyType_Type/PyInt_Type
2. Then, use PyTypeObject to initialize an object PyType_Type.
Code Location: Objects/typeobject. c
Definition
PyTypeObject PyType_Type = {PyVarObject_HEAD_INIT (& PyType_Type, 0) "type",/* tp_name */sizeof (PyHeapTypeObject),/* tp_basicsize */sizeof (PyMemberDef ), /* tp_itemsize */(destructor) type_dealloc,/* tp_dealloc * // method and attribute initialization value of the type object .....};
Description
1. tp_name
Type name. Here is "type"
2. PyVarObject_HEAD_INIT (& PyType_Type, 0)
PyVarObject_HEAD_INIT. This method is in Include/object. h,
Equivalent
Ob_refcnt = 1
* Ob_type = & PyType_Type
Ob_size = 0
That is, the type of PyType_Type is itself!
Structure
In the first figure, the arrow indicates instantiation (google doc is not very familiar with finding arrows of the corresponding type)
In the second figure, the arrow points
Use
#1. the int type is 'type'> type (int) <type 'type'> #2. the type is still 'type', corresponding to the second point described above >>> type (int) <type 'type'>
Note: at any time, ob_type points to the instance of PyTypeObject: PyType_Type/PyInt_Type...
3. Then, define the specific type. Here we use PyInt_Type as an example.
Code location Objects/objecbject. c
Definition
PyTypeObject PyInt_Type = {PyVarObject_HEAD_INIT (& PyType_Type, 0) "int", sizeof (py1_bject), 0, // method and attribute value of the int type .... (hashfunc) int_hash,/* tp_hash */};
Description
1. "int"
The type name of PyInt_Type is int.
2. PyVarObject_HEAD_INIT (& PyType_Type, 0)
PyInt_Type
*ob_type = &PyType_Type
Structure
Use
>>> type(1)<type 'int'>>>> type(type(1))<type 'type'>
4. Finally, an integer object int is generated.
Code Location Include/export bject. h
Definition
typedef struct { PyObject_HEAD long ob_ival;} PyIntObject;
Structure
Articles you may be interested in:
- Object Type in Python
- Python judges custom object types
- Python isinstance determines the object type