Python how to pass a variable by reference?

"" "
 # 1 function parameter passing Python 
here is to remember is the type belonging to the object, rather than a variable. 
And there are two objects," can change "(mutable) and" unchangeable "(immutable) objects in python in, strings, tuples, 
and the numbers are not subject to change, and List, dict, the SET and so it can be modified objects. (this is the focus of this issue) 
when a reference passed to the function, the function automatically copy references in this function references and references outside of the relationship is not half the hair. 
so the first case the function reference point to an immutable object when the function returns, the hair did not feel half outside references. the second not the same example, 
a reference point in the function of the object is variable, and its positioning on the operation of the pointer address as modified in memory. 
"" "
 # a = . 1 
# DEF Fun (a): 
Print # ( " func_in " , ID (A)) func_in # 1604579120 
# A = 2 
# Print ( " Re-Point " , ID (A), ID ( 2)) # Re-Point 1,604,579,152  1,604,579,152 
# 
# Print ( " func_out " , ID (A), ID ( . 1 )) # func_out 1604579120  1604579120 
# Fun (A) 
# Print (A) # . 1 

"" "
 Output 
func_out 1604579120  1604579120 
func_in 1,604,579,120 
Re -point 1604579152  1604579152 
1 

Process finished with Exit code 0 
"" "
 # all variables can be understood in a memory object" reference " 
# you can see, after executing a later = 2, a stored reference value, i.e., change the memory address, the address where the original object into a memory 2 address of the entity object.
"" "
 . Parameter assignment by passing the reasons behind this is twofold: 

parameter passing is actually a reference to an object (but the reference is passed by value) 
some data type is variable, but other variable data types are not 
so : 
If you will be a variable object to a method, the method will get a reference to the same object, you can change it, but if you re-referenced in the reference method, then it will be outside the scope of nothing know, after you're done, external references will still point to the original object. 
If the immutable object to the method, it is still unable to rebind external references can not even change the object. 
for more clearly, let us give some examples. 
"" " 
" ""
 list - variable type 
let us try to modify the list of transfer methods: 
"" "
 # DEF try_to_change_list_contents (the_list): 
# Print ( ' GOT ' , the_list) 
# the_list.append ( ' at Four ' ) 
# Print ( ' I changed to ' ,the_list)
#
# outer_list = ['one', 'two', 'three']
#
# print('before, outer_list =', outer_list)
# try_to_change_list_contents(outer_list)
# print('after, outer_list =', outer_list)

"""
输出
before, outer_list = ['one', 'two', 'three']
got ['one', 'two', ' Three ' ] 
changed to [ ' One ' , ' TWO ' , ' Three ' , ' Four ' ] 
After, outer_list = [ ' One ' , ' TWO ' , ' Three ' , ' Four ' ] 

"" "
 
# Because outer_list incoming parameter is a reference to it, rather than a copy of it, we can change it and make the changes reflected in the outer scope using the variation method list.
"""
现在让我们看看当我们尝试更改作为参数传入的引用时会发生什么:
"""
# def try_to_change_list_reference(the_list):
#     print('got', the_list)
#     the_list = ['and', 'we', 'can', 'not', 'lie']
#     print('set to', the_list)
#
# outer_list = ['we', 'like', 'proper', 'English']
#
# print('before, outer_list =', outer_list)
# try_to_change_list_reference(outer_list)
# print('after, outer_list =', outer_list)

"""
输出
before, outer_list = ['we', 'like', 'proper', 'English']
got ['we', 'like', 'proper', 'English']
set to ['and', 'we', 'can', 'not', 'lie']
after, outer_list = ['we', 'like', 'proper', 'English']
"""
Since the_list # parameters are passed by value, so that a new list will not affect the method code assigned to the outside. This is outer_list the_list reference copy, we the_list points to a new list, but there is no way to change the position of the pointing outer_list
"" "
 String - an immutable type 
it is immutable, so we can not change the contents of the string 
now, let's try to change the reference 
" ""
 
# DEF try_to_change_string_reference (the_string): 
# Print ( ' GOT ' , the_string) 
# the_string = ' the In A Kingdom by The Sea ' 
# Print ( ' SET to ' , the_string) 
# 
# outer_string = ' It WAS MANY and MANY A year ago Member ' 
# 
# Print ( ' before, outer_string = ' ,outer_string)
# try_to_change_string_reference(outer_string)
# print('After, outer_string = ' , outer_string) 
# 

"" "
 output 
before, outer_string = It WAS MANY and MANY A year ago Member 
GOT It WAS MANY and MANY A year ago Member 
SET to the In A Kingdom by The Sea 
After, outer_string = It WAS MANY and a year ago Member MANY
 "" "
 # Similarly, since the_string parameter is passed by value, it will not affect the outside to assign new method code string. This is a outer_string the_string reference copy, we the_string point to a new string, but there is no way to change the position of outer_string points.
= a 1 
a = 2 

"" "
 You think this is a memory location to store the value of 1, and then updated to store the value 2. This is not in Python work. 
Instead, as a reference to the object that has the value of start 1, and then be reassigned to an object having a reference to the value 2. the 
two objects may continue to coexist, even if a first object is no longer referenced; in fact, they may be any other references to the shared program. 
when the when you use the function is called, it creates a new reference to the incoming object references and function calls that use references are separate, and therefore can not be updated to reference the new object references in your case:.. 
"" "
 # DEF the __init __ (Self): 
# self.variable = ' Original ' 
# self.Change (self.variable) 
# 
# DEF Change (Self, var ): 
#      var = ' the Changed ' 


" ""
 self.variable is character string object reference ' Original '
When you call Change, create the object var second reference. 
Inside the function, you will be quoted reassigned to a different string object var ' Changed ' , but the reference self.variable is independent, it does not change. 
The only way to solve this problem is to pass a variable object. Because the two references refer to the same object, so any changes will be reflected in the two object position. 
"" "
 # DEF the __init __ (Self): 
# self.variable = [ ' Original ' ] 
# self.Change (self.variable) 
# 
# DEF Change (Self, var ): 
#      var [ 0 ] = ' the Changed ' 


" " "
 to the above comment: 
it is not passed by value, it is not passed by reference - it is called one by one. See Lundh Fredrik: 
HTTP: // effbot.org/zone/call-by-object.
This is an important quote: 
"Variable ...... [name] is not an object; they can not be represented by an object or referenced by other variables." 
In your example, when calling the method Change - to create a namespace; and var becomes the namespace name of the character string to be ' Original ' . 
Then, in the name of the object has two namespace. Next, var = ' the Changed ' to bind to a new var string object, so the method namespace forgotten ' Original ' . 
Finally, forget the name space, and the string ' the Changed ' used with it.
"" "
# x = [ 2, 4, 4, 5, 5 ]
# print (x)  # 2, 4, 4, 5, 5
#
# def go( li ) :
#   li = [ 5, 6, 7, 8 ]  # re-assigning what li POINTS TO, does not
#   # change the value of the ORIGINAL variable x
#   print("go_li",li)
# go( x )
#
# print (x)  # 2, 4, 4 , 5 , 5   [STILL! ] 
# 
# 
# Print ( ' Press the any Key to the Continue ' ) 


"" "
 interesting summary 
fact is that the entire reference / value concept will not fit python.Python no variable" value ".Python only a reference to the object and the object name. 

Therefore, when you call the function and place the "name" in parentheses, as follows: 

DEF FUNC (the X-): # Defines AN Takes a function that argument 
    ... # do something here Wallpaper 

FUNC (myname) # Calling at the function 
myname transfer points to the actual object, rather than the name myname itself. inside the function, gives another name (x) to refer to the same object passing. 

You can modify the object inside the function (if it is variable), but you can not change the name of the object pointed to by the external like you did that. 

anothername = myname 
So I can answer your question:

It was "passed by value", but all values are just referenced object. 

"" "
 # A = . 1 
# Print ( " A " , ID (A)) 
# DEF nuw_1 (Data): 
# Print ( " Data " , ID (Data)) 
# Data + = . 1 
# 
# Print ( " Data " , ID (Data)) 
#      return Data 
# 
# A = nuw_1 (A) 
# Print ( " A " , ID (A)) 


A = [] 
Print ( " A " , ID (A)) 
DEF nuw_1 (Data):data",data,id(data))
    data=1
    print("data",data,id(data))

nuw_1(a)
print("a",a,id(a))

"""
输出

  a 2072004795720
  data [] 2072004795720
  data 1 1604579120
  a [] 2072004795720

"""

a=[]
print("a",id(a))
def nuw_1(data):
    print("data",data,id(data))
    data.append(1)
    print("data",data,id(data))

nuw_1(a)
print("a",a,id(a))
"""

Output:

  a 2072004793288
  data [] 2072004793288
  data [1] 2072004793288
  a [1] 2072004793288

"""

 

Reproduced in: https: //www.cnblogs.com/Xingtxx/p/11044255.html

Guess you like

Origin blog.csdn.net/weixin_33892359/article/details/93224715