CPython Object Model: Basics

Source: Internet
Author: User

1 Preface

Recently read the "Python Source Code Analysis" a book, harvest quite abundant. Although the book has been written for a long time, the book said and today's implementation has a lot of different, but the procedural framework has not changed too much, supplemented by the Python official website document, still can take this glimpse of Python source code.

In accordance with this book in the process, was quite abundant, incisively and vividly recorded the thoughts of the experience, so began to write this blog. If I am lazy cancer is not seizure, then there should be a number of follow-up blog, may involve most of Python's built-in types of anatomy and even others; if the unfortunate lazy cancer attack, this blog is enough to record enough knowledge points in order to quickly recall the object model related.

The code in this article is based on the source code of the Python 3.4.2 .

2 Object Model Overview
    • This section is derived from include/object.h
2.1 Pyobject

Objects in Python have some common properties that are extracted and placed in a fixed structure: < Span class= "Linenr" >

struct _object {    _pyobject_head_extra    py_ssize_t ob_refcnt;     struct _typeobject *ob_type;} Pyobject; /* */

The members of the Pyobject are simple, a ob_refcnt to represent the number of references (note: Python implements the reference count garbage collection, which is used for this reference number.) The py_ssize_t can be thought of as a long, in short a shape), a ob_type to represent the object type. The content of the object type is mentioned in detail later in this article.

This is the generic representation structure for all objects in Python in the interpreter. Moreover, many functions in Python's C API also accept parameters of the pyobject* type.

2.2 Pyvarobject

In python, fixed-length objects also have variable-length objects (but in python3.4, there are some non-common-sense settings for variable-length and fixed-length, which is later), like a list is a typical variable-length object whose size depends on the number of elements and is unpredictable.

Variable-length objects also have some common features that are extracted and placed in the following data structures:

struct {    pyobject ob_base;     /*  */} pyvarobject;

The ob_size is the number of elements in the variable-length object, not the actual amount of space that the object occupies. And by including members of the Pyobject type, you actually implement a simple inheritance manually. To get a clearer view of this inheritance and its role in the object model, a diagram of the inheritance relationship is given in the following article.

2.3 pytypeobejct

PYTYPEOBEJCT is the data structure used to represent the object type, which is defined as follows: (most of the definitions have been omitted to save space)

typedefstruct_typeobject {pyobject_var_head/*a macro, expanded after: Pyvarobject ob_base;*/    Const Char*tp_name;/*for printing, in format "<module>.<name>"*/py_ssize_t tp_basicsize, tp_itemsize;/*For allocation*/    /*Methods to implement standard operations*/destructor Tp_dealloc;    Printfunc Tp_print; /*Method Suites for standard classes*/Pynumbermethods*Tp_as_number; Pysequencemethods*tp_as_sequence; Pymappingmethods*tp_as_mapping; /* ... */} pytypeobject;

The Pytypeobject structure contains the various types of data required for a type, as an example of the members listed in the code above:

    • Ob_base, its ob_base member (that is, ob_base.ob_base) is the object that represents a certain type (yes, each type is a special object)
    • Tp_basicsize, Tp_itemsize: Related to size of object of this type
    • Tp_name is the name of the type
    • Tp_dealloc is this type of destructor
    • Tp_as_number: As a function family of numeric types, such as subtraction

A number of function pointers are omitted from the code above, and those function pointers are the actions that some types need or may have.

Each of these types has a separate variable of type Pytypeobject. Note, however, that because Pytypeobject also contains a member of the Pyobject type, which needs to point to a variable of type pytypeobject, it is clear that this pytypeobject cannot be pointed to. In the case of the int type, if the Pytypeobject variable pointed to by the int type is itself, then the type representing the int type is the int type itself. But first of all, it is counterintuitive that the int type as an object of type and int has the same type as an object . There may be a few types that can do this, but the int type is obviously not the case, and the nature and operation of the int and int objects are very different and can never be the same type. Second, it does not match the result of the run, and if you run type (int) in Python, the int is not displayed and the type is displayed instead.

Therefore, in order to solve this problem, a meta-type pytype_type (that is, the source of type in the run type (int) result) is built into Python, and pytype_type is the type of the built-in type, which itself is the type itself.

2.4 Summary

At this point, the three most basic data structures in the Python object model are already covered, but the relationship between them and the types and objects in Python is unclear. Here's a picture:

, whether Pytypeobject or Pyvarobject, all contain members of the Pyobject type, as a result of which the inheritance is implemented manually by this method.

At the same time, every instance of the Pyobject type that exists has a single object, so even pytypeobject, which is used to represent a type, will eventually correspond to an object, except that the object is very special.

Finally, a simple object of type int in Python is used to illustrate the structure of the object model. Take a = Ten for example:

The Pylong_type is the structure of the int type after python3.0. This figure has some inappropriate places, 10 the object's content should be more complex, it should be inherited from the Pyobject object of a specialized type of instance. But for the sake of simplicity, this is represented in this nonstandard way.

CPython Object Model: Basics

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.