Let's "move" with Python.

Source: Internet
Author: User
Tags shallow copy

Let's "move" with Python.

  A python variable A is mentioned in all of the above objects, and it is specifically noted that it has a label: tag. In fact, whether it is called "variable" or "tag" does not have a specific requirement, called it "variable" is just the continuation of the programming habit, called it "tag" is determined by the dynamic characteristics of Python.

Here is a comparison of the C language "variables" with Python's "tags". C-language variables need to be a type declaration before use, and Python tags (variables) can be used directly, without the label (variable) type declaration, assign to the tag (variable) What type of object it is, and what type it is.

C Language:

Main () {    int  A;     Char C;         1 ;     ' s ' ;}

Python:

A = 1's'

Let's take a look at the results in the Python interactive command line:

The C language variable has an action that declares the variable type before it is assigned, in fact this action is to open up a corresponding size memory space for this variable type. Learn c all know the different types of variables whose data length is not the same, such as Integer variable int the data length is 4 bytes, and the character variable char data length is 1 bytes, this data length represents the size of its occupied space in memory. The C language effectively leverages memory space resources by declaring variable types. (the sizeof () function can be used to view the data length of a variable)

while Python assigns tags (variables) (Python interprets tags as Python's reference to objects is more intuitive), if the object being assigned is "referenced" for the first time, the Python (The exact python compiler) will open up a memory space for this object, and the size of the memory space Python will be automatically allocated based on the object's type and data content (Python's sys package has a The Getrefcount () method returns the number of references to an object. The following is a process diagram referenced by a Python object to complete today's description of Python's dynamic features:

Execute a = k equivalent to the object K at the door of the home stickers ad A, through this small ad a we can find the home of the object K, to find the object K, and then what to do with this object k to see what you want to do. and B = A is the equivalent of finding a small ad a, next to it affixed with a small ad B, or by B = k directly at the door of K and then affixed a small ad B, so that, whether through the ads A or small ads B can find the object K. While the ID (k)(built-in function in Python, which returns the address of the object in memory space) is the equivalent of the K's House registration information, the address of the object K can also be obtained by locating the housing registration information. Of course, there are also small ads can be torn off, but as long as there are small ads can find the object K's home according to small ads, if the small ads are torn out of that can not find the object K.

PS: The object reference mechanism for Python I am only introduced from the most understandable level of the surface, the shallow copy, the deep copy and the garbage collection mechanism that I want to cite do not appear here, and then we will introduce it separately according to the usage. Python's flexible object-referencing mechanism allows Python to "move" completely, and the little partner will no longer be bothered by declaring various types of variables.

  

Let's "move" with Python.

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.