python parameter passing is neither transmitting nor reference value transfer

[Body part]

Value passed (passl-by-value) during the formal arguments of the function transferred as a local variable called function processing, i.e., opened up in the stack memory space to store the value of the argument into the calling function, thereby I want to be a copy of the argument. Characteristic value is passed in the form of operating parameters of the called function are carried out as a local variable, the value does not affect the calling function argument variables. [Give you a value, you like to be a copy]

Reference transmission (pass-by-reference) process, the called function in the form of local variables as parameters although opened in the stack memory space, but at this time is the address placed by the calling function to the argument variables. Is any operation parameter of modulation functions are treated as indirect addressing, i.e. access the argument in the calling function by the variable address stored in the stack. Because of this, the parameter is to make any adjustment operations affect the function argument variables of the calling function. [Give you a copy of my address, I can find this at any time, you can have dinner at my house Han]

Glossary:
1, variables and objects
  in Python everything is an object, the object is digital, is a list of objects, functions are objects, everything is an object. Variable is simply a reference to the object, the operation target is accomplished by reference. The data type is for [target] in terms of, not for [variable].

2, reference:
  can be understood as the memory address of the object points.

3, the assignment
  nature assignment is to bind a new object, for immutable data types, the equivalent of a new object is bound, it is to be passed as a value of the object after completion of variable references assignment occurs Variety. args = 2 prior to execution, to get the value of the argument arg variable a, as a copy of the object, for arg assignment operation time, are only used as local variables, it will not affect the main function of the argument variable a.

def foo(arg):
    arg = 2 
    print(arg)
 
a = 1 
foo(a)  # 输出:2
print(a) # 输出:1

Here Insert Picture Description

After For variable data type, having a characteristic CRUD, CRUD performed when no assignment, it does not bind the new object, referred to the same reference the same object or CRUD, variable point or the object. Therefore, reference is made in the form of transfer. arg before performing the append, to address the argument variable b own object reference also gave arg, arg so the time for the operation of a reference object b has also been modified.

def bar(args):
    args.append(1) 
 
b = []
print(b)# 输出:[]
print(id(b)) # 输出:4324106952
bar(b)
print(b) # 输出:[1]
print(id(b))  # 输出:4324106952

Here Insert Picture Description

Conclusion:
  Python does not allow programmers choose to use pass by value or reference. python uses parameter passing is definitely "pass object references" approach. This is equivalent to an integrated manner by value and pass by reference. If the function is a received immutable objects (numbers, characters or tuples) of references, you can not directly modify the original object - by the equivalent of 'transfer value' to pass the object. If the function is to receive a variable object (dictionary, list) the reference value of the original object can be modified - the equivalent of 'pass by reference' to pass the object.

Published 217 original articles · won 125 Like · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104218229