The reference value is passed in the transfer Python

When the variable is immutable, the function call is passed by value
When a variable is an object variable, the function call is passed by reference
 
What is the value of delivery:
Value transfer process, the parameter function call is used as a local variable of the function processing, i.e. opened up memory space in the stack to store the value of the argument is placed by the main calling function, thereby becoming a copy of the argument
Characteristic value is passed to the called function of any operating parameter is carried out as a local variable, it does not affect the main function of the argument variables
def test1(c):
    print "test before"
    print id(c)
    c+=1print "test after "
    print id(c)
    return c

>>> c = 1
>>> test1(c)
>>> print c
1

 

What is passed by reference:
Reference transfer process, called function parameter as a local variable, although also opened in the stack memory space, but then is stored into the call by the main function of the address of a variable argument, the called function parameter any operations are treated as indirect addressing, the address is stored in the stack by accessing main argument variables of the function call, it will affect the value of the main function call arguments.
def test2(c):
    print "test before"
    print id(c)
    c.appned("1")
    print "test after "
    print id(c)
    return c

>>> c = [1]
>>> test2(c)
>>> print c
[1, "1"]
 
 
Conclusion: python does not allow programmers choose to use pass by value or reference, Python parameter passing used the "pass object references" approach,
This is equivalent to an integrated manner by value and pass by reference.
1) If the function is to receive a variable object (list, dict) reference can modify an object's original value, which corresponds to the object to pass through the "by-reference."
2) If the function is a received immutable, (int, tuple) the reference can not directly modify the original object, is transmitted through the equivalent of "traditional values" Object
 
 

Guess you like

Origin www.cnblogs.com/wangmengyu1993/p/12449114.html