Python language implementation

Source: Internet
Author: User

Recently looked at Chen Ru's "python Source code Anatomy", write very well, here simply record the Python language implementation of several points.

1. Python Object Implementation principle

First, the implementation of the object in Python, basically everything in Python is an object, and this object is based on Pyobject.

[Object. h]   struct _object {      int ob_refcnt;   Reference count      struct _typeobject *ob_type;  } Pyobject;  

As you can see, this data structure is very simple and a reference count is a type pointer.

This reference count is the basis of the garbage collection mechanism, just like the smart Pointer (shared_ptr) in C + + (there are 4 smart pointers!!). ), the number of references is 0 and is removed from the heap.

The type of the object is determined by this ob_type.

For example, creating a Pyintobject,python internally uses a pyobject* to maintain the variable, and when used, it goes to Ob_type to find the specified function. For example:

Long Pyobject_hash (Pyobject *v) {      *tp = v->ob_type;       if (Tp->tp_hash! = NULL             ) return (*tp->Tp_hash) (v);   

The key information for the type is stored in the Ob_type.

2, Pydirtobject

Dirt is a very high-turnout type in Python, and Python maintains a dict to hold member variables for each instance of the class.

Dict and C + + Map,set are associative containers, but their underlying implementation is different, C + + uses an optimized red-black tree, while Python uses a hash table, the search complexity from O (Logn) to O (1) (optimal), but it seems to need more space, We know that an instance of Hashtable in Java has two parameters that affect its performance: initial capacity and load factor . Python hasn't found-_-!.

3. Virtual machine

Python's virtual machines and java,c# have the same principle, so what does this so-called virtual machine explain? The following is an explanation of the Java Virtual machine

Virtual machine is an abstract computer, which is realized by simulating various computer functions on the actual computer. Java Virtual machine has its own perfect hardware architecture, such as processor, stack, register, etc., also has the corresponding instruction system. The JVM masks information that is relevant to the operating system platform, allowing Java programs to run without modification on multiple platforms by generating only the target code (bytecode) that runs on the Java virtual machine.

The bytecode generated by the Python front-end (Pycodeobject) is given to the virtual machine to run, and the virtual machine runs an object--pyframeobject, which is the runtime environment, including some namespace.

Python language implementation

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.