Python source code analysis Note 1-Python object first sight, python source code
The book address: http://www.jianshu.com/p/763f6cec7a9b
I have been working for two years, and I use python most. However, I am not sure about the internal mechanism of python. Every day I indulge in simple logic writing for adding, deleting, modifying, and querying. A lot of things don't need to be forgotten, such as the C language, which is exactly the same. The python source code is written in C. It is really good to analyze the python source code while being able to understand the basics of the C language. In addition, there are guidance from the python source code analysis by Chen Ru, and the analysis will not be brainless. It is expected to be a small achievement in a month's spare time.
1. Objects in python
In python, everything is an object, which corresponds to a struct in C language implementation. First of all, we should start with python built-in objects. The most basic types are pymongobject, PyStringObject, PyListObject, and PyDictObject. They belong to the int, string, list, And dict types respectively. After python2.2 has the new style class, these built-in objects are inherited from the object type, and the object corresponds to PyBaseObject_Type in the code. For example, if we assign a value to Statement a = 3, then a is a pyintbject object whose type is int. In the code, the corresponding PyInt_Type and PyInt_Type are also an object, which we call a type object. So what is the type of PyInt_Type? The answer is type, which corresponds to PyType_Type in the code. Of course, object is also a type object, and its type is also PyType_Type. In this layer, PyType_Type is also an object. What is its type? Yes, the answer is that its type is its own ,. Check the following verification code:
# Built-in object test In [1]: a = 3In [2]: type (a) Out [2]: intIn [3]: type (int) Out [3]: typeIn [4]: type (type) Out [4]: typeIn [5]: int. _ base _ Out [5]: objectIn [6]: type (object) Out [6]: type
First, we will analyze the structures of several basic built-in objects in the C language and several commonly used macros. For convenience, I also use the same version analyzed by Chen Ru, whose version is 2.5.6. download the source code from the official website.
// Built-in object basics # define PyObject_HEAD \ incluob_refcnt; \ struct _ typeobject * ob_type; # define PyObject_HEAD_INIT (type) \ 1, type, # define PyObject_VAR_HEAD \ PyObject_HEAD \ incluob_size; /* Number of items in variable part */# define Py_INVALID_SIZE (bytes)-1 typedef struct _ object {PyObject_HEAD} PyObject; typestrudef CT {PyObject_VAR_HEAD} PyVarObject T_VAR_HEAD const char * tp_name;/* For printing, in format "<module>. <name> "*/define tp_basicsize, tp_itemsize;/* For allocation */destructor resume; printfunc tp_print; setattrfunc resume; cmpfunc tp_compare; reprfunc tp_repr ;...} pyTypeObject; typedef struct {PyObject_HEAD long ob_ival;} py1_bject; typedef struct {PyObject_VAR_HEAD long ob_shash; int ob _ Sstate; char ob_sval [1];/* Invariants: * ob_sval contains space for 'ob _ size + 1' elements. * ob_sval [ob_size] = 0. * ob_shash is the hash of the string or-1 if not computed yet. * ob_sstate! = 0 iff the string object is in stringobject. c's * 'interned' dictionary; in this case the two references * from 'interned' to this object are * not counted * in ob_refcnt. */} PyStringObject;
As shown in the code,PyObject is the cornerstone of all Python objects. All subsequent objects have the same PyObject header, so we can see in the source code that all objects can pointThis is the frequently used polymorphism technique in object-oriented programming. In Python, function objects are also transmitted through PyObject *. Even if it is a Python object, the py1_bject * pointer is not used in the Code. This is also used to implement polymorphism. For example, the following function:
void Print(PyObject* object) { object->ob_type->tp_print(object);}
In addition, as mentioned in the code comment, the ob_size of a variable-length object refers to the number of elements, not the number of bytes.
2. python object reference count
The following is a list of several macro definitions (object. h) for reference counting of commonly used operation objects. Besides some code used for debugging, it is easier to understand the meaning of the Code. Py_NewReference indicates the reference count when the object is initialized. Py_INCREF and Py_DECREF are used to increase the reference technology and reduce the reference count respectively. From the code, we can see that python uses these macro operations to add and reduce references. ** note that when the object references ob_refcnt is reduced to 0, the object's destructor is called, and the Destructor does not necessarily call free to release the memory space, because frequent requests and releases of memory seriously affect the performance, as a result, we can see that python uses a lot of memory pool technology to improve performance.
It should be noted that the type object is not included in the reference counting rules. The pointer of each object to the type object is not considered as a reference of the type object, that is, it will not affect the reference counting of the type object, type objects will never be destructed.
#define _Py_NewReference(op) ((op)->ob_refcnt = 1)#define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op)))#define Py_INCREF(op) ((op)->ob_refcnt++)#define Py_DECREF(op) \ if (--(op)->ob_refcnt != 0) \ ; else \ _Py_Dealloc((PyObject *)(op))#define Py_CLEAR(op) \ do { \ if (op) { \ PyObject *tmp = (PyObject *)(op); \ (op) = NULL; \ Py_DECREF(tmp); \ } \ } while (0)/* Macros to use in case the object pointer may be NULL: */#define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)#define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)
3. Python Object Classification
Objects in python can be roughly divided into the following types:
-Numeric objects: such as integer, float, and boolean
-Sequence set objects: such as string, list, And tuple
-Dictionary object: such as dict
-Type object: for example, type
-Internal object: As shown in the code, function, frame, module, and method objects displayed later.
4 references
- Python source code analysis