Python Learning notes Finishing (iii) Introduction to dynamic types in Python

Source: Internet
Author: User

There is only one assignment model in Python
The case of a missing type declaration statement
In Python, types are automatically determined during the run, not through code declarations. This means that there is no need to declare a variable. Just remember that this concept essentially applies to variables, objects, and the relationships between them. Then the concept is easy to understand and grasp.
1. Variables, objects, and references
Variable creation: A variable that is created the first time the code assigns it a value. The subsequent assignment will change the value of the variable name that was created. Python detects variable names before the code is run and can create variables as initial assignments.
Variable type: A variable never has any type information or constraints associated with it. The concept of a type is present in an object rather than in a variable. The variables were originally generic. It simply refers to a specific pair of images at a specific point in time.
Use of variables: When a variable appears in an expression, it is immediately replaced by the current reference, regardless of the type of the object.
In addition, all variables must be explicitly assigned before they are used. Using a variable that is not assigned causes an error.
>>>a=3
Conceptually, Python will perform three different steps to complete the request.
1), create an object to represent the value 3
2), create a variable A, if it has not yet been created
3), connect the variable to the new object 3
The connection from a variable to an object in Python is called a reference. A reference is a relationship that is implemented in an in-memory pointer form.
* A variable is an element of a system table that has a connection space to an object.
* Objects are allocated pieces of memory and have enough space to represent the values they are representing.
* References are auto-formed pointers from variables to objects.
Each object uses two standard header information: A type marker to identify the type of the object, and a reference counter to determine whether the object can be reclaimed.
2. Type belongs to object, not variable
The type in Python is associated with an object, not with a variable.
The variable has no type and the variable points to the object. objects have types and know their type, each containing a header information that marks the type of the object.
3. Garbage collection of objects
What happened at the end of the object's life?
Whenever a variable name is assigned to a new object, the space occupied by the previous object is retracted (if it is not referenced by other variable names and objects). This technique of automatically reclaiming object space is called garbage collection.
Internally, Python accomplishes this by keeping the number of times that the counter in each object records references to that object. Once (and precisely at the same time) This counter is set to zero, the object's memory space is automatically retracted. The most straightforward and beneficial benefit of garbage collection is that it means that objects can be used arbitrarily in scripts without the need to consider freeing up memory space.
>>> x=42
>>> ID (x)
674748828
>>> x= "Diege"
>>> ID (x)
676367648

Second, share the reference
All of these are cases where a single variable is assigned a reference to multiple objects. Now, in interactive mode, introduce another variable and look at the variable name and object changes.
>>> a=10
>>> B=a
>>> ID (a)
674749212
>>> ID (b)
674749212
The second row uses Python to create the variable B. The variable A is used, and it is not assigned here, so it is replaced with its applied object 10, and B becomes a reference to the object. The actual effect is that both variables A and B refer to the same object (that is, they point to the same memory space.) Called a shared reference in Python--Multiple variable names apply the same object. )
>>> a=10
>>> B=a
>>> a= ' Diege '
>>> ID (a)
676367648
>>> ID (b)
674749212
Variable a changes, but does not affect the variable B. This completely illustrates that variable B is pointing to the object 10 memory space.
In Ptyhon, a variable is always a pointer to an object, not a label for the memory area that can be changed. Assigning a new value to a variable does not replace the original object, but rather allows the variable to refer to an entirely different object. The actual effect is to assign a value to a variable that only affects the assigned variable.
1. Share references and modify in situ
There are objects and types that actually change objects on the ground. For example, assigning an offset to a list does change the list object instead of generating a new list object.
>>> t1=[11,12,13]
>>> T2=T1
>>> T1
[11, 12, 13]
>>> T2
[11, 12, 13]
>>> t1=22
>>> T1
22
>>> T2
[11, 12, 13]
This is the same as the previous T1 changed T2 unchanged, T2 change does not affect T1
>>> t1=[11,12,13]
>>> T2=T1
>>> T1
[11, 12, 13]
>>> T2
[11, 12, 13]
>>> t2[1]=33
>>> T1
[33, 12, 13]
>>> T2
[33, 12, 13]
Found T2 changed, T1 also changed with this.
The same T1 changed, and T2 changed.
>>> t1[1]=99
>>> T2
[33, 99, 13]
>>> T1
[33, 99, 13]
Here T1 has not changed, changing an element of the object referenced by T1. This type of modification overrides a part of the list object. Because this list object is shared with other objects (referenced by other objects), a change like this will not only affect T1. It is important to realize that as such modifications, it affects the rest of the program.

If you do not want this to happen, Python is required to copy the object instead of creating a reference. Methods include the built-in list function and the copy module of the standard library, the most common way is the Shard t1[from beginning to end:]
>>> t1=[11,12,13]
>>> t2=t1[:]
>>> T1
[11, 12, 13]
>>> T2
[11, 12, 13]
>>> t1[0]=99
>>> T1
[99, 12, 13]
>>> T2
[11, 12, 13]
>>> ID (T1)
676366604
>>> ID (T2)
675542060
T1 and T2 point to different objects, so they don't affect each other.
Note: This shard technique does not refer to other mutable core types (dictionaries, because they are not sequences), and the D.copy () method should be used for dictionaries. Also, note that the copy module in the standard library has a generic copy of any call to an arbitrary object, and also a call to copy the structure of the nested object.
>>> x={' name ': ' Diege ', ' Age ': 28}
>>> Import Copy
>>> y=copy.copy (X)
>>> X
{' Age ': +, ' name ': ' Diege '}
>>> Y
{' Age ': +, ' name ': ' Diege '}
>>> ID (X)
676370468
>>> ID (Y)
676414436
>>> x={' name ': {' FirstName ': ' Diege ', ' LastName ': ' Wangkaijin '}, ' age ': 28}
>>> X
{' Age ': +, ' name ': {' LastName ': ' Wangkaijin ', ' FirstName ': ' Diege '}}
>>> y=copy.copy (X)
>>> Y
{' Age ': +, ' name ': {' LastName ': ' Wangkaijin ', ' FirstName ': ' Diege '}}
>>> z=copy.deepcopy (X)
>>> Z
{' Age ': +, ' name ': {' LastName ': ' Wangkaijin ', ' FirstName ': ' Diege '}}
2. Shared references and equality
>>> x=33
>>> x= ' Diege '
Because Python caches and re-uses small integers and small strings, as mentioned earlier, object 33 may not be retracted as it was said earlier, instead it will probably remain in a system table, waiting for the next time your code generates another 33来 exploit. As soon as possible, most kinds of objects will be recycled as soon as they are no longer referenced. For those that are not recycled, the buffering mechanism has nothing to do with the code.
Judging if they are equal
>>> l=[1,2,3]
>>> m=l
>>> l==m
True
>>> L is M
True
= = Checks if the object has the same value. Is operator to check the identity of the object. If two variable names point precisely to the same object, it returns True. So this is a more rigorous equivalence test.
It is, in fact, just a pointer to a realistic reference. So, if necessary, a way to detect shared references in your code. If the variable name reference values are equal. But for a different object, its return value will be false.
>>> l=[1,2,3]
>>> m=[1,2,3]
>>> l==m
True
>>> L is M
False
>>> ID (L)
676367788
>>> ID (M)
676367724
You can see two variables pointing to different objects by using the ID () function.
>>> x=33
>>> y=33
>>> x==y
True
>>> X is Y
True
>>> ID (X)
674748936
>>> ID (Y)
674748936
This is test returns true because small integers and strings are cached for reuse.
If you want to know more, you can query python for the number of times an object is applied: The Getrefcount function in the SYS module returns the number of times an object has been applied.
>>> Import Sys
>>> Sys.getrefcount (33)
13
>>> Sys.getrefcount (1)
427
>>> Sys.getrefcount (00)
296
>>> Sys.getrefcount (99)
6

Python Learning notes Finishing (iii) Introduction to dynamic types in Python

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.