Python Source code Analysis: The origin of Pyobject objects and the realization of polymorphism

Source: Internet
Author: User

In Python everything is an object, so how is this mechanism implemented? Let's look at this mysterious mechanism from the "source" of Python's lair.

typedef struct _OBJECT {

Pyobject_head

} Pyobject;

typedef struct {

Pyobject_var_head

} Pyvarobject;

See the two structures above? They are the source of Wanyuan, the ancestors of all Python objects, why are there two ancestors? This is going to be from the real time data objects, we have some data, such as numbers, fixed matrix, such as the number of elements is fixed, and such as strings, sets, dictionaries, such as the number of elements of the data will be floating up and down. In this way, the inventor of Python created two ancestors for both types of data when designing Python (like men and women, as if not quite, but essentially two ancestors): fixed-length data types and variable-length data types.

The two variables in these two structures are actually macros. From the source we can find the expansion of these macros:

#define PYOBJECT_HEAD \

_pyobject_head_extra \

py_ssize_t ob_refcnt; \

struct _typeobject *ob_type;

#define PYOBJECT_VAR_HEAD \

Pyobject_head \

py_ssize_t ob_size;

It can be seen that the ob_size of this type is a description of its variable length characteristics (number of elements).

Let's take a closer look at the contents of the Pyobject_head macro:

_pyobject_head_extra: This macro expands either the front and back pointers of the doubly linked list or is empty, and is used when tracking all objects: Imagine a two-way list that concatenates all of Python's objects in a series of scenes, so grand!

OB_REFCNT: This property records the number of times the object has been referenced by others (it is later found that this thing is used for memory management: a reference-counting garbage collection mechanism)

Ob_type: This thing is a play, it implements the Python polymorphism (what is polymorphic: for different types of objects, the same name method behaves differently), how can it be seen? Let's focus on this ob_type, and first look at what the Ob_type prototype is:

typedef struct _TYPEOBJECT {

Pyobject_var_head

const char *tp_name;

py_ssize_t Tp_basicsize, Tp_itemsize;

Printfunc Tp_print;

。。。

Pynumbermethods *tp_as_number;

Pysequencemethods *tp_as_sequence;

Pymappingmethods *tp_as_mapping;

。。。

} Pytypeobject;

This structure takes up 84 lines in the source code, the sky! It's a what, so much content. Let's focus on a few fields:

Pyobject_var_head

: Well, isn't this the variable length data type the head ' bone ' in its old ancestors? Why do you have it here? Yes, you guessed it, we indicated that the object prototype of an object type Pytypeobject is also an object. As for why it is the head ' bone ' of the object ancestor of the variable-length type, it is not clear that Chu ^0^.

Tp_print: The internal principle of polymorphism begins to reveal itself! This type object is used to provide different methods for different objects, as far as the name is used.

Pynumbermethods,pysequencemethods,pymappingmethods: In fact, like the above tp_print, only they are a function family (composed of function sets), class number operations (such as support +-x/), Class sequence operations (such as [n:m] slice operations), dictionary operations (such as the Dict[key] operation).

The following principles are validated on the Int type object:

typedef struct {

Pyobject_head

Long Ob_ival;

} Pyintobject;

This is our int type object, and its head? As can be seen in the Python source, a series of macros are used to initialize the head:

#define PYOBJECT_HEAD_INIT (type) \

_pyobject_extra_init \

1, type,

The type here, as seen from the source, has a pytype_type type, which is the object of the type that describes the type. It's a little bit around, but it's fine to understand it, nothing more: An int data that has a type object that specifically describes the data of type int, but not only type objects of type int but also type object of type string. Then you need to have an object to describe these types of objects, which is pytype_type, which means that it can test whether an object is a type object.

Python Source code Analysis: The origin of Pyobject objects and the realization of polymorphism

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.