For example, this code:
def add (num):
num= num+10
d=2
Add (d)
Print D
Output 2
What should I do if I want to pass the address of D as C so that it outputs 12?
The function of Python is assigned, and the assignment is implemented by establishing a variable with the object's association.
Executing d = 2 o'clock, you create D in __main__ and point it at 2 for this integer object.
Execute the function in the Add (d) procedure:
After D is passed to the Add () function, Num also points to the 2 in __main__ within the function.
After executing num = num + 10, new Object 12 is created, and Num points to the new object,--12.
If you understand the difference between a local variable in a function and a variable in a __main__, it is clear that in __main__, D is still pointing to the object 2, which does not change. So, you get 2 when you print D.
If you want to make the output 12, the simplest way is:
Add return num to function add ()
Use D = add when calling function (d)
The code is as follows:
def add (num): num + = ten return NUMD = 2d = Add (d) Print D
October yards
Links: https://www.zhihu.com/question/20591688/answer/24372602
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
This article is from the "Dream to Reality" blog, please be sure to keep this source http://lookingdream.blog.51cto.com/5177800/1917379
Python script function passing parameters