Python python dynamic type

Source: Internet
Author: User

In Python, the process of declaring a variable is omitted, and when a variable is referenced, a simple assignment statement is done at the same time, declaring the variable type, the definition of the variable, and the associated process, so how does the python variable complete the definition?

Dynamic type

Python uses dynamic typing and the polymorphism he provides to provide a concise and flexible basis for the Python language. In Python we do not declare the exact type of object being used. The so-called Python dynamic type is the type of object that is automatically determined during the program's run.

objects, variables, and references

When we assign a variable, we actually do a lot of things automatically in Python.

1. Create a variable: When the code is assigned to a variable for the first time, the variable is created, and in the subsequent assignment process, Python verifies the variable name before the code is run and can create the variable as the original assignment.

2. Variable declaration: The type in Python only exists in the object, not the variable, the variable is universal, he just at some time in the program refers to a certain type of object, such as the definition of a =1, a = ' a ', at the beginning of the definition of the variable A to point to the integer type of object, The variable then points to a variable of type string, which is visible, and the variable is a type that is not fixed.

3. Variable use: Variables appear in the expression will immediately be replaced by the object, regardless of what the object is the type, the variable must be defined before use.

It is important to note that variables must be initialized before they can be updated, such as the counter is initialized to 0 before it can be added.

That is, when we assign a value to a variable, such as A=3,python performs three different operations to complete the assignment.

1. Create an object representing 3,

2. If there is no variable A in the program, create it.

3. Connect the variable to object 3.

Variables and objects are connection relationships, they are stored in different places in memory, and if there is a list of such large objects, the object is also connected to the objects it contains. This connection from a variable to an object is called a reference.

  

A reference to a variable is implemented in an in-memory pointer form. Once the variable is used, Python automatically joins the object of the variable. Specifically:

1. The variable is the element of the system table, which points to the address space where the object resides.

2. The object is allocated a piece of memory, the address can be connected, there is enough space to represent the value of the object,

3. The referenced procedure automates the process of pointing a variable to an object's address, which is a pointer from a variable to an object.

Garbage Collection of objects

Each object has two standard header information, one is a type marker for the object type, and the other is a reference counter that determines whether the object is recyclable. It is clear that only objects in Python have category distinctions, that variables are simply references to objects, that variables do not have categorical distinctions, and that they simply refer to a particular object at a particular time.

The use of reference counters is associated with the garbage collection mechanism of Python, and when a variable name is given a new object, the address space occupied by the previous old object is reclaimed. The space of the old object is automatically placed in a pool of memory space, waiting for later objects to be used.

How does the counter work in the garbage collection process? The counter records the current number of references to the object, and if the counter is set to 0 at a time, it means that the memory space of the object is not referenced and the name is retracted.

The garbage collection of objects is of great significance, which makes it possible to use objects in Python arbitrarily and without having to consider freeing up space, eliminating the need for a lot of basic code in C and C + +.

Shared references (reasons for a deep copy)

When a variable uses more than one object, the old object is garbage retracted, so what kind of object does the variable share variable have?

A= ' Hello World ' b=aprint (b) Run Result: Hello World

It is worth mentioning that the B variable refers to a as a value, according to the assignment in Python is done by the object, so B should refer to the value of the object address that the a variable points to, so it can be judged that change a point to the object does not affect the value of B.

A= ' Hello World ' b=aa= ' new Hello World ' Print (a) print (b) Run result: new Hello Worldhello World

  

In Python, a variable is always a pointer to a specified object, rather than a label that changes the memory area, assigning a new value to a variable instead of replacing an object's original value, instead creating a new object for the variable reference.

Of course, this is limited to the type of the object can not be changed, if the reference object is like a list of objects available to modify the result?

A=[1,2,3]b=aa.append (4) print (a) print (b) Run result: [1, 2, 3, 4][1, 2, 3, 4]

As a result, for a mutable type object, the variable does not create an object, but instead inherits the previous object, even if the object has been changed. It can be simply understood that two objects point to the memory address of a list, and the list maps the memory address of each element, the sharing of variables is not concerned with the change of the list, they only care about the change of the memory space of the list, so the Mutable object can change itself in reference, so there is no need to create a new object. So the shared object will change with the previous object.

This is actually what we do not want to see. We can create references using copy objects:

A=[1,2,3]b=a[:]a.append (4) print (a) print (b) Run result: [1, 2, 3, 4][1, 2, 3]

This method does not apply to non-indexed but mutable dictionaries and collections, so Python's copy module is used for variable references:

Import Copya=[1,2,3,[1,2]]b=copy.copy (a) c=copy.deepcopy (a) d=aa.append (4) a[3][0]=5print (a) print (b) print (c) Print ( D) Operating results: [1, 2, 3, [5, 2], 4][1, 2, 3, [5, 2]][1, 2, 3, [1, 2]][1, 2, 3, [5, 2], 4]
Supplemental to shared references

is actually a little bit of garbage collection, for some small integers or strings, not as we say, the counter mark is 0 to be retracted. This is related to the Python caching mechanism. For a generic object, Python is suitable for garbage recall.

A=[1,2,3]b=[1,2,3]c=aprint (a = c) print (a = b) print (A is C) print (A is B) operation result: Truetruetruefalse

For small integers and strings, it is different:

A=111b=111c=aprint (a = c) print (a = b) print (A is C) print (A is B) operation result: Truetruetruetrue

In Python, everything works in assignments and references, and for understanding Python dynamic types, it can be helpful to work and learn in the future, which is the only assignment model for Python, so it is necessary to understand and apply it accurately. No type constraints, which makes Python code extremely flexible, efficient, and saves a lot of code, extremely concise, so python is such an art programming.

Python python dynamic type

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.