For example, Python references and objects.
Today, I saw such a strange sentence:In python, the variable names and objects are separated.At the beginning, I didn't respond to this sentence. I decided to clarify the details between variables and objects in python. (In fact, I think it is more appropriate to separate references from objects)
Start with the initial variable:
In python, if you want to use a variable, you do not need to declare it in advance. You only need to assign a value to the variable when you are using it (this is different from a static type language such as C language, python is a dynamic type ).
Example 1:
A = 1
This is a simple value assignment statement. integer 1 is an object, and expression a is a reference. Using the value assignment statement, expression a points to object 1: this process is equivalent to "flying a kite". Variable a is the "line" in your hand. python is like the "line", and uses references to touch and hold the kite in the sky-object.
You can use python's built-in function id () to view the identity of an object. This so-called identity is actually the memory address of the object:
Note:
Python is an object concept, so a function is also an object. Therefore, you can use the _ doc _ method of the id () function to view the specific description of this function.
Here is an example:
>>> a = 1>>> id(a)24834392>>> a = 'banana'>>> id(a)139990659655312
In the first statement, 2 is an integer object stored in the memory. a is referenced by a value to point to object 1;
In the second statement, a string object 'banana 'is created in the memory, and reference a is directed to 'banana' through a value assignment. At the same time, object 1 does not point to it by reference, it will be released by the python memory processing mechanism when I recycle garbage.
Another example:
>>> a = 1>>> id(a)24834392>>> a = 'banana'>>> id(a)139990659655312
In the first statement, 2 is an integer object stored in the memory. a is referenced by a value to point to object 1;
In the second statement, a string object 'banana 'is created in the memory, and reference a is directed to 'banana' through a value assignment. At the same time, object 1 does not point to it by reference, it will be released by the python memory processing mechanism when I recycle garbage.
Another example:
Use the function to view the references of variables a and B:
123456 |
>>> a = 3 >>> b = 3 >>> id (a) 10289448 >>> id (b) 10289448 |
Here we can see that these two references point to the same object. Why? This is related to the memory mechanism of python, because for languages, objects are frequently destroyed and created, which is especially a waste of performance. Therefore, in Python, integers and short characters are cached by Python for reuse.
Then let's look at this example:
1. a = 4
2. B = a (here let reference B point to the object that references)
3. a = a + 2
View references using functions:
When Step 1 is executed, check the references of a and B:
123456 |
>>> a = 4 >>> b = a >>> id (a) 36151568 >>> id (b) 36151568 |
We can see that both a and B point to the integer object 4.
Next we will point to Step 2:
12345 |
>>> a = a + 2 >>> id (a) 36151520 >>> id (b) 36151568 |
We can see that the reference of a has changed, but the reference of B has not changed. a and B point to different objects. In the second sentence, a is assigned a value again, let it point to the new object 6; even if multiple references point to the same object, if a reference value changes, it actually points to a new reference, does not affect other references. In terms of effect, each reference is independent of each other and does not affect each other.
There is also an example that must be viewed:
L1 = [] a = 0for I in range (): a = I l1.append (a) # Add the print (l1) object pointed to by a # [1, 2, 3, 4] l2 = [] B = [1, 2, 3] for I in range (1, 5): B [1] = I l2.append (B) # Add the object pointed to by B, which includes the reference of the list element. The list itself has not changed, but the object pointed to by list item [1] has changed print (l2) # [[1, 4, 3], [1, 4, 3], [1, 4, 3], [1, 4, 3] # Not expected [[1, 1, 1, 3], [1, 2, 3], [1, 3, 3], [1, 4, 3]
Therefore, the same object is added to each list.
L2 = [] B = [1, 2, 3] for I in range (1, 5): B [1] = I l2.append (copy. copy (B) # l2.append (copy. deepcopy (B) and copy. copy () results are the same as print (l2) # [1, 1, 3], [1, 2, 3], [1, 3], [1, 4, 3]
Copy. copy. Only the parent object is copied, and the internal sub-objects of the object are not copied.
So what is the difference between copy. copy () and copy. deepcopy?
l2 = []b = [1,[4,5],3]for i in range(1,5): b[1][0] = i l2.append(copy.copy(b)) # [[1, [4, 5], 3], [1, [4, 5], 3], [1, [4, 5], 3], [1, [4, 5], 3]] # l2.append(copy.deepcopy(b)) # [[1, [1, 5], 3], [1, [2, 5], 3], [1, [3, 5], 3], [1, [4, 5], 3]]print(l2)
Copy. deepcopy () deeply copies the object and its sub-objects.
Last example:
(This example involves variable data types and immutable data types in python ):
Before starting this chestnut, remember to note the difference between the fourth Chestnut.
1. L1 = [1, 2, 3]
2. L2 = L1
3. L1 [0] = 10
View references using functions:
When you perform steps 1 and 2, check the L1 and L2 references:
123456 |
>>> L1 = [ 1 , 2 , 3 ] >>> L2 = L1 >>> id (L1) 139643051219496 >>> id (L2) 139643051219496 |
At this time, L1 and L2 are referenced in the same way. They all point to the list object [1, 2, 3.
Next, continue to Step 1:
1234567 |
>>> L1[ 0 ] = 10 >>> id (L1) 139643051219496 >>> id (L2) 139643051219496 >>> L2[ 10 , 2 , 3 ] |
In the same way as the fourth Chestnut, the value of one of the objects is modified, but the result is not the same as that of the fourth Chestnut. In this experiment, the reference of L1 and L2 has not changed, but the value of the list object [, 3] has changed to [, 3] (the list object has changed)
In this case, we no longer assign values to the reference L1, but assign values to the elements of the table pointed to by L1. The result is that L2 also changes.
Why? Because L1 and L2 points to the table that has not changed. A table actually contains multiple referenced objects (each reference is an element, such as L1 [0], L1 [1]..., each reference points to an object, such as 1, 2, 3 ),. The value assignment operation L1 [0] = 10 does not change L1's point, but instead changes L1 [0], that is, a part of the table object (an element ), so all references to this object are affected.
(In contrast, our previous assignment operations did not affect the object itself, but changed the reference point .)
The list can reference its elements to change the object itself (in-place change ). This type of object is called a mutable object. The dictionary is also of this type.
Like the previous numbers and strings, the object itself cannot be changed, but the reference point can only be changed, called immutable object ).
The previously learned tuple can call referenced elements but cannot assign values. Therefore, it cannot change the object itself, so it is also an immutable object.
Thank you for reading the python article of Shanghai shangxue. For more articles and technical support, please click Shanghai Python training.
Related Articles are recommended: character encoding, interactive input, and String concatenation in Python basic learning; * args and ** kwargs in Python; and function parameter passing in Python.