Python Learning notes (v) Python dynamic types and references

Source: Internet
Author: User
Tags type null

The dynamic type and the polymorphism provided by him are the basis for the simplicity and flexibility of the Python language. In Python, the type is automatically determined during the run and there is no need to declare the variable beforehand. Remember that variables and objects in Python are separated so that dynamic types can be clearly understood. Remember, the assignment generates a reference to the object, not a copy.

1, single variable reference

Take A=3 as an example to illustrate the processing of a single variable reference in Python,

1) Create an object representing the value 3, here is the integer numeric object

2) If variable A has not yet been created, create variable a (no this step is created)

3) Connect the variable to the new object 3

As shown in the actual effect, the variable A and object 3 are stored in different parts of memory, connected by a connection (the variable name and the variable value in the C language is one), the connection from the variable to the object is called the reference, and the reference is the automatically formed pointer from the variable to the object.


In Python, the variable name has no type, it belongs to the elements of the system table, the same variable can refer to different object types (the variables in Python are similar to the void * type null pointer in C, which can point to any data type). Object want $ type, each object has two standard header information: A type identifier represents an object type, and a reference counter to determine whether the object can be reclaimed (the counter is a 0 o'clock recycle object). Whenever a variable is pointed to a new object, the object's memory space is automatically reclaimed by Python-garbage collection-if the object's reference number previously pointed to is 0. It is this garbage collection mechanism that makes it possible for Python to use objects without worrying about memory release, which eliminates the need for a large number of base code.

2, shared reference

1) shared references to immutable objects (numbers, strings, tuples)

When multiple variables refer to the same object, there is essentially no relationship between the variables, and a reference to the same object is created respectively. Changing the reference of any one variable does not affect other variables.

A=3

B=a

Parsing the above statement illustrates the handling of shared references, where Python will create variables A and B to point to the same integer object 3 (that is, the reference counter is 2), as in.

If you type the statement a=a+2, the variable a points to the new object 5, does not change the value of Object 3 (the integer object is immutable), nor does it change the reference to variable B.


A= ' spam '

After Python executes the previous sentence, the references to variables A and B become:

2) shared references to mutable objects (lists, dictionaries)

A shared reference to a Mutable object must be used with caution, because the modification of a variable (and, of course, the value of the object) also affects other variables, similar to a function reference in C or multiple pointers pointing to the same memory. The copy module of the standard library can be used to copy an object.

Python Learning notes (v) Python dynamic types and references

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.