The Python language appears in many languages. There are many problems in our continuous learning and usage. Next we will learn the relevant knowledge and how to use the C language environment in detail. I hope you will have some gains.
- Exploring a variety of Python languages
- Describes various modules in Python
- Interpreting the processing file in Python
- Introduction to the Python language
- Analysis of simple and convenient Python language
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 our computer language are only 0, 1 sequences 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 structure in C on Heap.
In order to implement the object-oriented mechanism of Python in C language, some structures need to be defined to operate the memory space of those objects.
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;
- 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 reference-based garbage collection mechanism in Python.
Another is a pointer to a struct of the type object to represent the type of the object.
In the C language implementation, there is also a struct extended to PyObject
That is, PyVarObject, whose content is the macro PyObject_VAR_HEAD, which has an ob_size more than PyObject,
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 structure, the initial part of its memory will have several variables of the above structure, 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.
Another thing I just didn't talk about 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.
C code
- 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;
The above is the usage of the Python language in the C language environment.