Python object system in-depth analysis

Source: Internet
Author: User
This article mainly introduces the Python object system and provides an in-depth analysis in the form of examples. if you need it, you can refer to this article to analyze the Python object system in detail. Share it with you for your reference. The details are as follows:

Guido uses C language to create Python. in the Python world, everything is an object.

1. Python objects in the C perspective

Let's trace back to the source, Python is implemented by the C language, and provides the c api http://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 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 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.

The code is as follows:

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 Python's 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 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 greater than PyObject to indicate the length of the object. for details, see the 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.

The code is as follows:

Typedef struct _ typeobject {
// Note that the start part is PyObject_VAR_HEAD.
PyObject_VAR_HEAD
Char * tp_name;/* For printing, in format
" . "*/
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.

The code is as follows:

PyAPI_DATA (PyTypeObject) PyType_Type;/* built-in 'type '*/
PyAPI_DATA (PyTypeObject) PyBaseObject_Type;/* built-in 'object '*/

The object is a basic object in Python. its structure in C language is PyBaseObject_Type. from the name in C language, we can probably know that this class is a type object.

In Python Corresponds to PyType_Type in C

The code is as follows:

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.

The code is as follows:

Typedef struct {
PyObject_HEAD
Long ob_ival;
} Py1_bject;

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

The code is as follows:

YTypeObject PyInt_Type = {
PyObject_HEAD_INIT (& PyType_Type)
0,
"Int ",
Sizeof (pyequalbject ),
......
};

4. Custom object

When we create a Python object, it is ultimately implemented through the underlying Python layer,

After we define A class A by using the Python language, Python first creates A class object (class object) like A according to the code you write ), then, when you need to create an instance of A, in fact, the underlying Python is actually 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.

Note:

The definition of C is as follows (python inherits from a class and is directly written in parentheses after the class name ):

The code is as follows:

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:

Type is the class of all classes.

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.