1, the modification of parameters
Normal use
Tuples, strings, int types are immutable, passed in as arguments, and do not change their own values
list, the dictionary is a mutable data structure, passed in as a parameter, and if the function executes, the value itself is changed by the action inside the function.
Exceptional cases
If you want to modify an immutable data structure:
The value can only be re-assigned by the value returned by the function.
If you do not want to change the parameters of a mutable data structure:
Can only copy the structure, name[:]
2. Keyword parameters and default values
The parameters are passed by default, depending on where the function is defined, or you can specify it manually:
def hello (greeting,name): print "%s,%s!"% (greeting,name) Hello (greeting= "Hello", name= "Qujun")
3. Collect parameters
When there are a large number of arguments that need to be passed, the parameters of the function can be defined using *params, such as:
def muli_paramter (*params): Print params
Note: Multiple parameters collected in this way will be stored in tuple format
To collect the keyword parameters, you need to use the **params
def print_params (x,y,z=3, *pospar,**keypar) L print x, y, z print pospar print Keypar
Note: The parameters collected by **params are dictionary format
4, the inverse use of parameters
def add (x, y): return x + yparams= (*params) def hello (greeting= "Hello", name= "Qujun"): print "%s,%s!"% (Greeti ng,name) keypar={' name ' = "Xixi", "greeting" = "haha") Hello (**keypar)
This article is from the "Danielqu" blog, make sure to keep this source http://qujunorz.blog.51cto.com/6378776/1905246
Python functions using parameter tip notes