Guido rosum, the father of Python, used his own Christmas holiday to build the Python language. In the Python world, objects have a wonderful "meaning ".
1. Python objects in the C perspective
Let's trace back to the source. Python is implemented in C language and provides C's APIhttp: // docs.python.org/c-api/index.html.
When we think about the problem, it may be easy to understand the object, but the computer can only understand the byte sequence such as the 0, 1 series. Basically, the objects in the computer language we call are only the 0, 1 sequence in a memory space in the memory. These continuous or non-continuous memory spaces can be considered as a whole at a higher level.In Python, the general objects we mentioned are a piece of memory space applied by the struct in C on Heap.
To implement the Python object-oriented Mechanism in C language, you need to define some struct to operate the memory space of those objects.
1. PyObject & PyVarObject
All Python objects share something in common. We can abstract them into a structure PyObject.
- Typedef struct _ object {
- PyObject_HEAD
- } PyObject;
- // In fact, the macro PyObject_HEAD is
- Int ob_refcnt;
- Struct _ typeobject * ob_type;
Ob_refcnt is the object reference count. It exists to implement the Python reference-based garbage collection mechanism. Another is a pointer to a struct of the type object to represent the type of the object.
In the implementation of C language, there is also a struct extended to PyObject, that is, PyVarObject, whose content is the macro PyObject_VAR_HEAD. It has an ob_size larger than PyObject to indicate the length of the object, for more information, see http://docs.python.org/c-api/structures.html.
Another point is to avoid obfuscation. Here, PyObject and PyVarObject have no correspondence with real objects in the Python world. These two are only an abstraction of all Python objects in the C language representation. that is to say, in C language, as long as it is the data of a Python object struct, the initial part of its memory will have several variables of the struct above, therefore, a pointer to a PyObject can point to all structures in C that represent Python objects. In this way, in the implementation of C, we can use this unified pointer to operate all the built-in Python object structs.
2. PyTypeObject
Another thing I haven't mentioned just now is the structure of _ typeobject (PyTypeObject), which is the abstraction of all types of objects in Python, in this way, we can use the PyTypeObject pointer to call all types of object structures at the C language level.
- Typedef struct _ typeobject {
- // Note that the start part is PyObject_VAR_HEAD.
- PyObject_VAR_HEAD
- Char * tp_name;/* For printing, in format
- "<Module>. <name> "*/
- Int tp_basicsize, tp_itemsize;/* For allocation */
- /* Methods to implement standard operations */
- Destructor tp_dealloc;
- Printfunc tp_print;
- ......
- /* More standard operations (here
- Binary compatibility )*/
- Hashfunc tp_hash;
- Ternaryfunc tp_call;
- ......
- } PyTypeObject;
3. Correspondence between Python built-in objects and C struct
Now, the object and type abstraction of the Python object-oriented mechanism have been mentioned. Next, let's take a look at how the actually existing objects in python are implemented in C language?
The first thing to talk about is the built-in Python objects, which are defined in C language. After the Python environment is initialized, these objects are created.
- PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
- PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
The object is a basic object in Python. in C language, the corresponding struct is PyBaseObject_Type. From the name in C language, we can probably know that this class is a type object.
In Python, <type 'type'> corresponds to PyType_Type in C.
- PyTypeObject PyType_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0, /* ob_size */
- "type", /* tp_name */
- sizeof(PyHeapTypeObject), /* tp_basicsize */
- sizeof(PyMemberDef), /* tp_itemsize */
- ……
- };
Let's take a look at the specific integer.
The struct of an integer instance in C is py1_bject.
- typedef struct {
- PyObject_HEAD
- long ob_ival;
- } PyIntObject;
That is to say, with this struct, we can point to the Python integer object in the C language runtime.
Then the corresponding Python Integer type object is
- yTypeObject PyInt_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0,
- "int",
- sizeof(PyIntObject),
- ……
- };
4. Custom object
When we create A Python object, it is all done through the underlying Python. After we define A class A by using the Python language, python first creates A class object such as A according to the code you write), and then when you need to create an instance of, in fact, the bottom layer of Python is created through the Class Object.
Ii. Object System from the Python perspective
In a simple Python world, everything is an object. These objects can be divided into three types,
Metaclasses, classes, instance
Classes can be divided into built-in type and user-defined class.
The following is a detailed description of the image.
The definition of C is as follows (python inherits from a class and is directly written in parentheses after the class name ):
- class C(object):
- ......
The solid line indicates the is-kind-of relationship, and the dotted line indicates the is-instance-of relationship.
When you view the base class of the current classes object (the instances object does not have the _ bases _ attribute), you can use classes_name. _ bases _. The value is a Tuple (Python supports multi-inheritance ).
The method for viewing the type of the current object is object_name. _ class __
We can use some tests to confirm the above figure:
Here, type is the class of all classes.