Value passing and reference passing in Python (mutable and immutable objects) is the principle of assignment-python is all reference passing

Value passing and reference passing in Python (mutable and immutable objects) is the principle of assignment-python is all reference passing

20141215 Chenxin

Guess:
1. Objects belonging to the same class, the default properties point to the same reference. This way, when you modify an object, it will affect other objects, unless you modify it through other methods in the class. In fact, all should be It is the concept of pointers.
2. The basic "variable" is the immutable "object", which is the value of the call. When you re-assign the value "=", Python creates a new value (object) internally Give it. (Here is what is commonly referred to as value transfer). Do n’t care what kind of transfer is, bluntly, Python is all reference transfer.
3. For "mutable objects" such as list, dict and default objects, completely It is a way of passing by reference. It is a reference to an address in memory, like a pointer.
4. If you want to copy an object to other objects (in the form of a copy), then it can only be achieved through Python's copy module.
5.Python has a cache The concept of cached integer range is (-5, 256). Other types will not be cached. For example, x = 1, y = 1, x is y; x = 257, y = 257, x is not y; id (*) To check the memory address.

Test:
The explanation of function parameter passing is as follows (3 examples): <<< Python Basic Tutorials Second Edition >> P93
def ChangeInt (a):
a = 10
b = 2
ChangeInt (b)
print b #-> 2
translates to The non-function looks like:
b = 2
a = b #The memory address of a is the same as b, indicating that this is a copy of the memory address, so the assignment can be understood as a pointer pointing to
a = 10 #pointing a to the newly created 10 object, The memory address of a has changed.
print b

def ChangeList (a):
a [0] = 10
b = [2]
ChangeList (b)
print b #-> [10]
translates to non-function:
b = [2]
a = b # a's memory address is the same as b The same, indicating that this is a copy of the memory address, so the assignment can be understood as the pointer points to
a [0] = 10 #point the value of the first element of the list pointed to by a new 10 object created in memory, the memory address of a has not changed, It is still the same as the memory address of b. What is changed here is only the address of the first element in the list, and the address of a is not changed (that is, the address of the entire list pointed to by a).
Print b

def ChangeList (a):
a = [10]
b = [2]
ChangeList (b)
print b #-> [2]
translated into non-function:
b = [2]
a = b # a's memory address is the same as b Yes, it means that this is a copy of the memory address, so the assignment can be understood as a pointer pointing to
a = [10] # Recreate a new list, a points to this new list, and the memory address of a has changed.
Print b

Conclusion:
Summing up the above 3 examples, it can be understood as:
1. The assignment in Python "=" statement can be understood as the pointing of the pointer.
2. Because of the pointer pointing problem, the concept of "scope" was derived instead of The above result caused by the "artificial limitation" of the scope, right ?!
3. Pay attention to the difference between variable objects and immutable objects. List, dict, int, char, tuple ...
4. This is also launched The concept of value passing and reference passing can be uniformly understood as reference passing in Python.

Explanation:
The actual situation of int immutable:

The actual situation of variable list:

Ways of passing reference of class object and instance object:

!/bin/env python

class
class_a (): num_a = 'abc' #What about when the string is long enough? Will it be a Python cache? The effect is the same, not cache
a = class_a
b =
class_a a.num_a = 'xyz' #No matter the length of the string is the same,
print a.num_a, b.num_a #Output xyz xyz
print id (a.num_a), id ( b.num_a) #Output the same memory address
In this class, no matter how to change the a object, the b object will change accordingly.
Explain that the object is a variable object. The class object is just an initial memory space, and then the instance object will be generated Fill in the values.? ? ?

<< End >>
Knowledge

In fact, there is not much difference in python. The main thing is that Python creates an object by assignment.
So for objects in python, a = '1' can't be called modification, only creation. If you think this is modification, you are wrong.
Once the assignment statement is used, it is basically independent of the original object. #Actually a pointer re-pointing

And Python also has mutable objects and immutable objects:
like simple types, integers, strings, tuples are immutable.
And like list, dict, object are variable. So the modification of them is generally to call the corresponding method, for example, you need a.append (b) for list, and a ['b'] = '1' for dict.
The object itself does not change, its properties or values ​​change. For all these cases, passing by value or by reference is the same.

However, it may be said that the reference is more accurate, for example:
def b (c):
print id (c)
a = [2]
print id (a)
b (a)
You will see that the id value printed out is the same twice . The description is by reference.
In other words, the object pointed to by the parameter in function b is a.
It does not matter to pass by value, because from the perspective of assignment, the assignment in the function will create a new object without affecting the original parameters.

It is more reasonable to use the concept of pointers to understand.

In fact, the variable name and the real object in Python is a binding relationship, or reference relationship. Therefore, it is more appropriate to understand the reference.

Unlike other languages, Python does not allow programmers to choose whether to pass values ​​or references when passing parameters. Python parameter passing must use the "pass object reference" method. In fact, this method is equivalent to a combination of passing by value and passing by reference. If the function receives a reference to a mutable object (such as a dictionary or list), you can modify the original value of the object-equivalent to passing the object by "passing by reference." If the function receives a reference to an immutable object (such as numbers, characters, or tuples), you cannot directly modify the original object-equivalent to passing the object by "passing by value".

Python generally assigns variables internally, it is a reference variable, which is similar to the concept of C address. You can use id () to query the memory address.
If a = b, the addresses of a and b are the same;
if you just want to copy, then you must use b = a [:], so that the memory addresses of a and b are different Over

a = [1,2]; b = a [:]; #here a and b both point to a memory address called list, the list stores the memory addresses corresponding to the specific values. The list is a double-layer address;
id (a)
140337215511368
id (b)
140337215510288 #different

a = 'abc'; b = a [:]; #Here is a string form, a points to the memory address of 'abc', after b = a [:] (equivalent to b = a), b also points to The memory address of 'abc'. The string is a single-layer address; the wording here is a bit polymorphic.
id (a)
140337216460600
id (b)
140337216460600 #same

Reference refers to the saved value as the address of the object. In the Python language, the values ​​held by a variable are referenced except for the basic type which holds the value, so you need to be careful about their use. Here is an example:
Problem description: Know a list, and seek to generate a new list. The list element is a copy of the original list.
A = [1,2]
b = a
This approach does not actually generate a new list. What b points to is still the object pointed to by a. In this way, if the elements of a or b are modified, the values ​​of a and b change at the same time.
The solution is:
a = [1,2]
b = a [:] In
this way, modifying a has no effect on b, and modifying b has no effect on a. (The copy of the list, that is, the copy. Not by reference)
But this method is only suitable for simple lists, that is, the elements in the list are basic types. If the list elements still exist in the list, this method is not applicable. The reason is that a process like a [:] just generates a new list of the value of the list element. If the list element is also a list, such as: a = [1, [2]], then this copy is for the element The processing of 2] just copied the reference of [2], but did not generate a new list copy of [2].
If you solve this problem, you can use the deepcopy function in the copy module. The modified test is as follows:
import copy
a = [1, [2]]
b = copy.deepcopy (a)
print b-> [1, [2]]
a [1] .append (3)
print a-> [ 1, [2, 3]]
print b-> [1, [2]]
Sometimes it is very important to know this, because you may indeed need a new list, and operate on this new list, and do not want to affect the original list.

Guess you like

Origin www.cnblogs.com/chanix/p/12737696.html