Python source code Analysis----Object structure

Source: Internet
Author: User

Although I can spit out the language of Python is slow, but also have to say that the Python language in the syntax of refinement, when writing code is really more convenient than Java ... But since Python can't begin to define a set of strongly-typed interfaces like Java, it's always a little bit less practical to write Python ... But pycharm to some extent through the comments can also alleviate a bit of this concern.

All right..... decided to look at the implementation of Python ... Let's start with the object structure of Python ....


In Python, all things are objects, integers are, methods are,. Anyway, everything is ....

Looking at Python's code implementation, you can see the pointer type everywhere: pyobject*, so let's take a look at Pyobject how exactly this is defined:

typedef struct _OBJECT {  //This is the basis for all Python objects, this is the immutable object    Pyobject_head} pyobject;

He is the topmost object structure in the entire Python environment, very simple, including an object header, so look at its definition:

Through this structure, all objects are formed into a doubly linked list/* Define pointers to the support a doubly-linked list of all live heap objects. */#define _pyobject_head_extra                struct _object *_ob_next;               struct _object *_ob_prev; #define _pyobject_extra_init 0, 0, #else # define _pyobject_head_extra#define _pyobject_extra_ init#endif//the data common to all objects, this is placed at the very beginning of all object memory, first the front and back pointers that make up the list of objects, followed by the number of references, and its type Object/** (1), which forms the circular list (2) reference count (3) type Object **//* Pyobject_head defines the initial segment of every pyobject. */#define Pyobject_head                       _pyobject_head_extra                    py_ssize_t ob_refcnt;                   struct _typeobject *ob_type;


As can be seen, the head is composed of 3 parts, the first is a front and back pointers for the object to form a two-way list, followed by the more important reference count ... And then there is the type object----"Each object has a pointer to the type object it is pointing to ...


Now that you see the reference count, let's say a python gc ... The biggest difference between it and Java in GC is a set of memory collections based on reference counts ...

(1) Each object is associated with a reference count, and when the count is 0, its memory can be recycled.

(2) For cases with circular references, Python also has a set of GC for accessibility analysis to reclaim memory. Of course, it's much simpler than Java.


The most basic pyobject structure seen above ... There is another structure here: Pyvarobject, which is used to refer to some variable-length objects, mainly some containers ....

It's usually some kind of container. typedef struct {        //Variable object base    pyobject_var_head} pyvarobject;//Even mutable objects, first there is a uniform object header, Ob_ Size is typically used to record the number of data items in a container, note that this is not a byte # # Pyobject_var_head                   pyobject_head                           py_ssize_t ob_size;/* Number of items in Variable part */#define PY_INVALID_SIZE (py_ssize_t)-1

It actually extends a 0b_size field ... In essence, it is also unified with Pyobject .... So for Pyvarobject, you can also use the Pyobject pointer to refer to it. So as to achieve a unified ....


From the above, we know that all objects will have a Ob_type pointer field that points to the type object of the current object, and then look at the definition of the type object:

This is used to specify the type object of an object typedef struct _TYPEOBJECT {pyobject_var_head//Here is a variable-length object Head const char *tp_name;/* for Prin Ting, in format "<module>.<name>"///for printing the current type of information py_ssize_t tp_basicsize, tp_itemsize; /* For allocation *///For allocated size/* Methods to implement standard operations *///The following is the definition of the standards method destructor Tp_deallo    C       Destructors the current object Printfunc tp_print;   When the print type is called, it will be called here for output getattrfunc tp_getattr;   getter function Setattrfunc tp_setattr;       Setter function Cmpfunc Tp_compare;         comparison function Reprfunc Tp_repr;      Convert to String/* Method Suites for standard classes */Pynumbermethods *tp_as_number;  The most numeric function of the operation Pysequencemethods *tp_as_sequence;    A function Pymappingmethods *tp_as_mapping as a sequence of operations;                   functions as a dictionary */* More standard operations (here for binary compatibility) */Hashfunc Tp_hash;                The function of obtaining hash value ternaryfunc Tp_call;    Reprfunc Tp_str when called as a method; Getattrofunc Tp_getattro;    Setattrofunc Tp_setattro;       /* Functions to access object as Input/output buffer */Pybufferprocs *tp_as_buffer;    Method for handling the current object as buffer */Flags to define presence of optional/expanded features */long tp_flags; const char *tp_doc; /* Documentation String *///current type of document/* Assigned meaning in Release 2.0 */* Call function for all accessible obj    ECTS */Traverseproc Tp_traverse;                                /* Delete references to contained objects */Inquiry tp_clear;    /* Assigned meaning in Release 2.1 */* Rich comparisons */Richcmpfunc tp_richcompare;    /* Weak reference enabler */py_ssize_t Tp_weaklistoffset;    /* Added in Release 2.2 *//* iterators */Getiterfunc Tp_iter;    Iternextfunc Tp_iternext;          /* Attribute descriptor and subclassing stuff */struct pymethoddef *tp_methods;          methods struct Pymemberdef *tp_members;    Member attribute struct Pygetsetdef *tp_getset;    struct _typeobject *tp_base;         Parent type Pyobject *tp_dict;    Descrgetfunc Tp_descr_get;    Descrsetfunc Tp_descr_set;    py_ssize_t Tp_dictoffset;    Initproc Tp_init;    Allocfunc Tp_alloc;    Newfunc tp_new; Freefunc Tp_free; /* Low-level free-memory routine */Inquiry tp_is_gc;    /* for PYOBJECT_IS_GC */Pyobject *tp_bases; Pyobject *tp_mro;    /* Method Resolution ORDER */Pyobject *tp_cache;    Pyobject *tp_subclasses;    Pyobject *tp_weaklist;    destructor Tp_del; /* Type attribute Cache version tag. Added in version 2.6 */unsigned int tp_version_tag; #ifdef Count_allocs/* These must is last and never explicitly I    nitialized */py_ssize_t Tp_allocs;    py_ssize_t tp_frees;    py_ssize_t Tp_maxalloc;    struct _typeobject *tp_prev; struct _typeobject *tp_next; #endif} pytypeobject;

As you can see, there are a lot of common functions defined here .... At the same time, we can see at the beginning that he also defines a variable-length object's head Pyobject_var_head


That is to say, the type object itself is an object, you can also use the Pyobject pointer to reference ....


Well.. So.. Here's the problem .... Since the type object is also an object .... So what is the type object of his own type object?

Well... A top-level type object is defined in Python .....

The topmost type object, which makes a self-reference of a type Object Pytypeobject Pytype_type = {pyvarobject_head_init (&pytype_type, 0) "type", /* Tp_name */sizeof (PYHEAPTYPEOBJECT),/* tp_basicsize */sizeof (Pymember                                          DEF),/* tp_itemsize */(destructor) Type_dealloc,/* Tp_dealloc */0,                                          /* Tp_print */0,/* tp_getattr */0, /* Tp_setattr */0,/* tp_compare */(REPRF UNC) TYPE_REPR,/* TP_REPR */0,/* tp_as_number */0 ,/* Tp_as_sequence */0,/* tp_as_ma Pping */(HASHFUNC) _py_hashpointer,/* Tp_hash */(TERNARYFUNC) Type_call,/* TP          _call */0,                                /* TP_STR */(GETATTROFUNC) Type_getattro,/* Tp_getattro */(Setattro    Func) Type_setattro,/* Tp_setattro */0,/* tp_as_buffer */ Py_tpflags_default |        py_tpflags_have_gc | Py_tpflags_basetype | Py_tpflags_type_subclass,/* tp_flags */type_doc,/* Tp_doc */(TRAVERSEP ROC) Type_traverse,/* tp_traverse */(Inquiry) Type_clear,/* tp_clear */Type_        Richcompare,/* Tp_richcompare */offsetof (Pytypeobject, Tp_weaklist),                                          /* Tp_weaklistoffset */0,/* tp_iter */0,                               /* Tp_iternext */type_methods,/* tp_methods */type_members,                        /* Tp_members */type_getsets,       /* Tp_getset */0,/* tp_base */0,                                          /* Tp_dict */0,/* tp_descr_get */0,                                  /* Tp_descr_set */offsetof (Pytypeobject, tp_dict),/* Tp_dictoffset */Type_init,                                   /* Tp_init */0,/* tp_alloc */type_new, /* tp_new */Pyobject_gc_del,/* tp_free */(Inquiry) TYPE_IS_GC ,/* TP_IS_GC */};

Here you can see that the type object itself refers to itself ....

As you can see later, all Python type objects point its own type object to the Pytype_type


All right.. Basically a rough understanding of the object structure of Python .....

Python source code Analysis----Object structure

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.