Python source Learning (i)

Source: Internet
Author: User

In view of the performance requirements, I used the most in the work of C + +, however, the work often have some validation work, the performance requirements are not high, but the efficiency of the completion of higher requirements, for such work, with a development of high-efficiency language is a reasonable idea, Given the popularity of Python and the development efficiency and flexibility that I've been praising, I'm starting to get more and more in touch with Python. However, the problem comes again, after the test with Python, you need to optimize the performance, or even ported to C + +, this time, it becomes increasingly necessary to learn more about Python, understanding its internal principles. After a search, found this "Python Source code Anatomy", began my python in-depth learning journey.

A preliminary study of Python objects

Everything in the Python language, including variables and variable types, is implemented by some object/struct in C. It can also be understood that all of the things in Python are implemented using the C object (which is understood here as the object of the struct). All of these objects correspond to the definition, and the beginning part is the same, all pyobject:

[objectstruct  _object {    pyobject_head} pyobject;
[Object. h] #ifdef py_trace_refs/*Define pointers to support a doubly-linked the list of all live heap objects.*/    #define_pyobject_head_extrastruct_object *_ob_next; struct_object *_ob_prev; #define_pyobject_extra_init 0, 0,#else    #define_pyobject_head_extra#define_pyobject_extra_init#endif/*Pyobject_head defines the initial segment of every pyobject.*/#definePyobject_head \_pyobject_head_extraintob_refcnt; struct_typeobject *ob_type;

The integer variable ob_refcnt is related to Python's memory management mechanism, which implements a garbage collection mechanism based on reference counting. While Ob_type is a pointer to the _typeobject struct, the _typeobject struct defines the type of object to which it is directed, defined as follows:

[Object. H]typedefstruct_typeobject {pyobject_var_headChar*tp_name;/*for printing, in format "<module>.<name>"*/    intTp_basicsize, Tp_itemsize;/*For allocation*/    /*Methods to implement standard operations*/destructor Tp_dealloc;    Printfunc Tp_print; .../*More standard operations (here for binary compatibility)*/Hashfunc Tp_hash;    Ternaryfunc Tp_call; ......} Pytypeobject;

That is to say, pytypeobject this struct defines the type of object in Python's C code.

Python source Learning (i)

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.