Let's look at the relationship between variables and objects: Everything in Python is an object, a number is an object, a list is an object, a function is an object, everything is an object. While a variable is a reference to an object (also known as a name or a label), the object's operation is done by reference. For example, a = [] is an empty list object, and variable A is a reference to that objectExample 1
def Test (c): C.append ("helloWorld"printreturn = []test (list) print (List,id (list))
Output [1, 2, ' Hello World '] 2463790879240[1, 2, ' Hello World ' 2463790879240
before executing the test function, the list and parameter C both point to the same object, the test is executed without re-assignment, there is no new pointing procedure, the Append method simply inserts an element into the list object, the object is the original object, but the contents of the object are changed, Because both the parameter C and list are bound on the same object, the C.append and List.append methods are essentially manipulating an object, so the list is changed after the function is called, but the ID does not change, or the original object. Therefore, if a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference"Example 2
def"i in test2"print"Hello word "test2 (str)print(Str,id (str))
Output: I in test2 2885210784112hello word 2885210784048
ID is different, so it is not the same object, that is, we pass the reference. Therefore, if a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value"。Summary:python parameter passing is definitely a way to "pass an object reference". This approach is equivalent to a synthesis of the value of the pass and the reference. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference". If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value". Reference: http://www.cnblogs.com/loleina/p/5276918.html
Whether the Python pass is a value or a reference