"Python Source code Anatomy"

Source: Internet
Author: User

1. Python Built-in objects

An object is a collection of data and operations based on that data, and in Python, an object is a piece of memory that is requested on the heap for the struct body in C.

In Python, once an object is created, its size in memory is constant, and objects that need to accommodate variable-length data can only maintain a pointer to a variable-sized area of memory within the object.

The cornerstone of 1.1.1 object mechanism--pyobject
[object.h]typedef struct _object{    int ob_refcnt;                        /* 引用计数器 */    struct _typeobject *ob_type;          /* _typeobject这个结构体用来指定一个对象类型的类型对象 */    } PyObject    

In Python, the core of the object mechanism is very simple, one is the reference count, the other is the type information

In Pyobject, you define the content that every Python object must have, and in addition to pyobject, there should be some extra memory to place additional information.

1.1.2 Fixed-length objects and variable-length objects Python integer objects Pyintobject
typedef struct{    PyObject_HEAD    long ob_ival;    } PyIntObject

The special information for an integer object is a shape variable in c that can be stored in this shaping variable, regardless of the value of the integer object.

Variable Length Object Pyvarobject
#define PyObject_VAR_HEAD     PyObject_HEAD             int ob_size;                           /* 容器中元素的数量 */    typedef struct{    PyObject_VAR_HEAD    } PyVarObject;    

We treat objects that do not contain variable-length data as "fixed length objects", and objects such as String objects that contain variable-length data are called "variable-long objects", and they differ in that the different objects of a fixed length object occupy the same amount of memory, while different objects of variable-length objects may not occupy the same memory.

Pyvarobject is just an extension of pyobject, within Python, each object has the same object header.

1.2 Type Object

When creating an object, you must know how much space to apply, and the amount of space required is a meta-information of the object, which is closely related to the type of object to which it belongs. Let's examine the type Object _typeobject:

typedef struct _typeobject {    PyObject_VAR_HEAD    char *tp_name;                          /* tp_name 类型名 */    int tp_basicsize, tp_itemsize;          /* 创建该类型对象时分配内存空间的大小 */        /* 与对象关联的操作信息 */    destructor tp_dealloc;    printfunc tp_print;        /* 更多操作信息 */     hashfunc tp_hash;    ternaryfunc tp_call;    ...    } PyTypeObject;    

A pytypeobject is the realization of the concept of "class" in the object-oriented theory of Python.

Creation of 1.2.1 Objects

Python methods for creating objects:

    1. Using the Python C API to create
    2. Created by type Object Pyint_type

Python provides a C API to allow users to interact with Python from the C environment, and Python's C API is divided into two categories:

    1. Generic API, or AOL
    2. Another class called Col
Create an Integer object from Pyint_type

Description: After Python finishes initializing the runtime environment, the symbol ' int ' corresponds to the Pyint_type object within Python, and object corresponds to the Pybaseobject_type object

    1. When you create an object using init (10), the Tp_new action in the Pyint_type object is called
    2. If this tp_new operation does not exist and is null, then go to the Tp_base specified base class to find the tp_new operation
    3. In the new class after Python2.2, all classes are based on object, so you will eventually find a non-null tp_new
    4. Tp_new points to Object_new,object_new, which accesses the tp_basicsize information recorded in Pyint_type, gets the amount of memory that needs to be requested, and then completes the request memory operation. As we said earlier, "in Python, the object is a piece of memory requested on the heap by the struct in C," so the memory request completes to represent that the object has been created (but not initialized)
    5. Tp_new after the object is created, the process turns to Pyint_type Tp_init, completing the initialization work

When we create an integer object 10 o'clock with an int (10), the first tp_new in Pyint_type is called, and if the tp_new is null, then the tp_new operation is found in the base class Tp_base the specified

Behavior of the 1.2.2 object

A large number of function pointers are defined in Pytypeobject, which end up pointing to a function, or to NULL, which can be thought of as defined in the type object, which directly determines the behavior of an object as it behaves at run time.

For example, Tp_hash in Pytypeobject is a variable of a function pointer (hashfunc) type, Tp_hash indicates how its hash value is generated for an object of that type.
The different operational information specified in Pytypeobject is also the key to distinguish one object from another.

Of these operations families, there are three groups that are very important:

Tp_as_number
指向PyNumberMethods函数族,该族定义了一个数值对象应该支持的操作,典型对象如int,tp_as_number.nb_add指定了对该对象进行加法操作时的具体行为。
Tp_as_sequence
指向PySequenceMethods函数族,该族定义了作为一个序列对象应该支持的操作,典型对象list.
Tp_as_mapping
指向PyMappingMethods函数族,该族定义了作为一个关联对象应该支持的操作,典型对象dict。

For a type, it is perfectly possible to define all the operations in the three function families at once, as long as the corresponding Special_method is defined

Types of 1.2.3 Types

In Python, the type is actually an object, and the type class corresponds to the pytype_type inside of Python, which is the class of all classes, called Metaclass, the Meta class in Python.

Leave a question here????????????????????
[object.h]#ifdef Py_TRACE_REFS    #define _PyObject_EXTRA_INIT 0,0,#else    #define _PyObject_EXTRA_INIT#endif#define PyObject_HEAD_INIT(type)    _PyObject_EXTRA_INIT 1, type
Foreign article: #define学习

The \ Backslash extends the definition to the next line, the compiler converts multiple physical rows into a logical line, and deletes the \ symbol

#define预处理器指令以 # As the beginning of a line, the instruction can appear anywhere in the source file, where the definition is valid from where the instruction appears to the end of the file.
Each line of # define (logical lines) is made up of 3 parts:

    1. The 1th part is the # define directive itself
    2. Part 2nd is the selected abbreviation, also known as a macro , and some macros represent values, which are referred to as class object macros
    3. The 3rd part is called the replacement list or replacement body

The process of turning from a macro to a final replacement text is called a macro expansion
Use parameters in # define to create a class function macro that is similar in shape and function to functions

"Python Source code Anatomy"

Related Article

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.