Python source code profiling note 1--python objects

Source: Internet
Author: User

Python source code profiling note 1--python objects

Work whole two years, with Python most, however for Python internal mechanism is not necessarily all clear, indulge in the simple logic of adding additions and deletions every day to write, really exhausting. Many things do not forget, such as C language, exactly, Python source code written in C, the analysis of Python source code at the same time can be warm C language Foundation, is really a good thing. In addition, there are Chen Ru the great God's "Python Source Code Analysis" to guide the analysis will not be unthinking. Expect to be in the one months of spare time, can be small, with this as a record.

1 objects in Python

In Python, everything is an object that corresponds to a struct in the C language implementation. First of all, of course, from the Python built objects start to look, the most basic is Pyintobject, Pystringobject, Pylistobject, Pydictobject these, they belong to int,string, list, The dict type. After python2.2 with the new style class, these built-in objects are inherited from the object type, and object corresponds to Pybaseobject_type in the code. For example, our assignment statement a=3, then A is a Pyintobject object, its type is int, in the code corresponds to Pyint_type,pyint_type is also an object, we call the type object. So Pyint_type what is its type, and the answer is type, which corresponds to Pytype_type in the code. Of course object is also a type, and its type is pytype_type. So a layer down, Pytype_type is also an object, then what is its type, yes, the answer is that its type is its own,. Look at the following verification code:

# #内建对象测试In [1]: a =3in [2]: type(A) out[2]:intin [3]: type(int) out[3]:typein [4]: type( type)out[4]:typein [5]:int. __base__out[5]:Objectin [6]: type(Object) out[6]:type

First analysis of the next several basic built-in objects in the C language of the structure and a few commonly used macros, for convenience, I use is also the Chen Ru of the Great God Analysis of the same version, version is 2.5.6. The source code has been downloaded.

//Built-in Object Foundation#define PYOBJECT_HEAD \py_ssize_t ob_refcnt;struct_typeobject *ob_type;#define PYOBJECT_HEAD_INIT (type) \        1, type,#define PYOBJECT_VAR_HEAD \Pyobject_head py_ssize_t ob_size;/* Number of items in variable part * /#define PY_INVALID_SIZE (py_ssize_t)-1typedef struct_object {pyobject_head} pyobject;typedef struct{Pyobject_var_head} Pyvarobject;typedef struct_typeobject {Pyobject_var_headConst Char*tp_name;/ * for printing, in format "<module>.<name>" * /py_ssize_t Tp_basicsize, Tp_itemsize;/ * for allocation * /destructor Tp_dealloc;        Printfunc Tp_print;        Getattrfunc tp_getattr;        Setattrfunc tp_setattr;        Cmpfunc Tp_compare;        Reprfunc Tp_repr; ...} Pytypeobject;typedef struct{Pyobject_headLongOb_ival;} Pyintobject;typedef struct{Pyobject_var_headLongOb_shash;intOb_sstate;Charob_sval[1];/* Invariants: * Ob_sval contains space for ' ob_size+1 ' elements.     * Ob_sval[ob_size] = = 0.     * Ob_shash is the hash of the string or-1 if not computed yet. * Ob_sstate! = 0 IFF the string object is in stringobject.c ' s * ' interned ' dictionary;     In this case the both references * from ' interned ' to this object is *not counted* in ob_refcnt. */} Pystringobject;

As shown in the code,Pyobject is the cornerstone of all Python objects, and all subsequent objects have an identical pyobject header, so we can see in the source that all objects can be pointed with the pyobject* pointer , This is the most frequently used polymorphic technique in object-oriented. Python internal function objects are also passed through pyobject*, even if this is a pyintobject type of object, the code will not be passed with the pyintobject* pointer, which is also to achieve polymorphism. For example, the following function:

voidobject) {    object->ob_type->tp_print(object);}

In addition, as commented in the code, the ob_size of the variable-length object refers to the number of elements, not the number of bytes.

2 Python object reference count

Here are a few of the commonly used operand reference count macro definitions (object.h), which are listed here, removing some of the code used for debugging, which makes it easier to see what the code means. Py_newreference is a reference count that is set when the object is initialized, Py_incref and py_decref are used to increase the reference technology and reduce the reference count, respectively. As you can see from the code, Python adds references and reduces references through these macros, but one thing to note is that when the object reference ob_refcnt is reduced to 0 o'clock, the destructor of the object is called, and the destructor does not necessarily call free to release the memory space. Since the frequent application and release of memory severely affect performance, it is later seen that Python is heavily used in memory pool technology, which has a great effect on improving performance.

It is necessary to note that the type object is not in the reference count rule, and that each object pointer to the type object is not considered a reference to the type object, that is, the reference count of the type object is not affected, and the type object will never be destructor.

#define _py_newreference (OP) ((OP)->ob_refcnt = 1)#define _PY_DEALLOC (OP) (* (OP)->ob_type->tp_dealloc) ((Pyobject *) (OP) )#define PY_INCREF (OP) ((OP)->ob_refcnt++)#define PY_DECREF (OP) \        if (--(OP)->ob_refcnt! = 0);Else                                                        _py_dealloc( (Pyobject *) (OP))#Define Py_clear(OP)                                     Do{if (OP){Pyobject*tmp=(Pyobject *) (OP);(OP)=NULL;Py_decref(TMP); }                                       } while (0)/*Macros  to  Use inch  Case  the Object Pointer  May  be NULL: */#Define Py_xincref(OP) if ((OP) = = NULL);Else Py_incref(OP)#Define Py_xdecref(OP) if ((OP) = = NULL);Else Py_decref(OP)
3 Python Object categories

The objects in Python can be broadly divided into the following categories:
-Numeric objects: such as Integer,float,boolean
-Sequence Collection object: such as String,list,tuple
-Dictionary object: such as Dict
-type object: such as type
-Internal objects: Code,function,frame,module and Method objects, as you will see later.

4 references
    • "Python Source code Anatomy"

Python source code profiling note 1--python objects

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.