The ing between Python built-in objects and C struct is quite useful to us to some extent. Next we will look at how we can better use this advantage, and hope you will gain some benefits. The first thing to talk about is the Python built-in objects defined in C language. After the Python environment is initialized, these objects are created.
Now, the object and type abstraction of Python's built-in 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 following is a reference clip:
- 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 addition, the <type 'type'> In the Python built-in object corresponds to the PyType_Type in the C language.
The following is a reference clip:
- PyTypeObject PyType_Type = { PyObject_HEAD_INIT(&PyType
_Type) 0, /* ob_size */ "type", /* tp_name */ sizeof
(PyHeapTypeObject), /* tp_basicsize */ sizeof(PyMembe
rDef), /* tp_itemsize */ …… };
Let's take a look at the specific integer.
The struct of an integer instance in the C language is py1_bject. The following is a reference segment:
- typedef struct {
- PyObject_HEAD
- long ob_ival;
- } PyIntObject;
That is to say, through such a struct, we can point to the Python integer object in the C language runtime .. The corresponding Python Integer type object is:
- yTypeObject PyInt_Type = {
- PyObject_HEAD_INIT(&PyType_Type)
- 0,
- "int",
- sizeof(PyIntObject),
- …… };
Python built-in object 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 underlying layer of the Python built-in object is created through the Class Object.