This article is for Python to encounter questionable features.
The following paragraph is the original basic tutorial Python function.
passing parameters by value and passing parameters by reference
All parameters (arguments) are passed by reference in Python. Assuming that you have changed the parameters in the function, the original parameter is changed in the function that called the function. Like what:
#!/usr/bin/python # writable function Description def changeme (mylist): "Change incoming list" mylist.append ([1,2,3,4]); Print "Function inside value:", MyList return # call changeme function mylist = [10,20,30];changeme (mylist);p rint "function outside the value:", MyList
The same reference is used for incoming functions and objects that add new content at the end. Therefore, the output results are as follows:
Values in function: [10, 20, 30, [1, 2, 3, 4]] values outside the function: [10, 20, 30, [1, 2, 3, 4]]
good. See here. Try it Yourself, code:
def printme (AGE,STR): str = ' str changed! ' Print age,str returnstr = ' ori str ' age = 23printme (age,str) Print str
Output Result:
23°c str Changed!ori STR
It seems to be wrong! Does not say that the function changes the value of the parameter, then the actual number of parameters will change?!
Is it not equal to strings and lists, etc.?
experienced people know that in Python, strings, tuples, and numbers are immutable objects, while list,dict are objects that can be changed.
So, remember the words in red, when you want to change an immutable object. In fact, it opens up a new object's storage space, which is why there is a global scope and a local scope for the problem.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Python what is a value or reference function parameter