The road to wudao-five-day Refining
Function:
Role: code reusability, data consistency, portability and expansion.
In python3.x, both the process and function are defined by def function name (form parameter). The difference is that the function has a return value, but the process does not, but the default return value is None, return can return multiple values separated by commas (,). return can also end function execution.
Function call: first define the function, and the corresponding passing parameters can be called (generally, the number of defined parameters must be greater than or equal to the number of passed real parameters, because there may be default parameters, except for parameter groups, that is, * args (tuples) and ** kwargs (dictionary). parameters are transmitted by location and keywords,
Parameter group: this parameter group is mainly used when you are not sure about the number of input parameters, or you want to directly input tuples or dictionaries.
Default parameter: The parameter has a default value. If this parameter is not set, the parameter is calculated based on the default value. Otherwise, the parameter is calculated based on the input value.
Example: def test (x, y, z ):
'''Write comments here '''
Print (x, '---', y, '---', z)
Return 'test ----'
Call location-based parameter passing: test (2, 4, 5)-> corresponds to x, y, z
Keyword parameter passing: test (y = 2, x = 4, z = 8), find the corresponding parameter name and assign a value
When the two parameters are mixed, the parameter passing by the location is in front (the parameter passing must be corresponding), for example, test (2, z = 6, y = 3)
* Args:
Def test1 (x, * args ):
Print (x, '---', args)
Return 'test1 ----'
Test1 (2, 4, 5, 6)-> x = 2 args :( 2, 4, 6); that is, to convert unnecessary parameters into tuples.
Test1 (2)-> x = 2, args :(). If this parameter is left blank, test1 (x = 3, * [2])-> x = 3, args :( 3,); test1 (6, * ()-> x = 6, args)
** Kwargs:
Def test3 (x, y = 6, ** kwargs ):
Print (x, '---', y, '---', kwargs)
Return '20140901'
Test3 (6, name = 'ddd ', kk = 'gg')-> x = 6, y is 6 by default, kwargs bit {'name': 'dd ', 'kk ': 'gg'}, that is, to convert the passing of multiple keywords into the dictionary corresponding to key => value. Note: it is recommended that you do not specify the same name as other parameters for the key to be passed in the dictionary. Otherwise, an error occurs when multiple parameters are passed in.
Test3 (y = 3, x = 5, mm = 'fff')-> x = 5, y = 3, kwargs: {'mm': 'fff '}; test3 ()-> x = 7, y = 1, kwargs :{}; test3 (x =, ** {'name': '55 '}) -> x = 3, y = 3, kwargs: {'name': '55 '}
3. x global variables and local variables:
Global variables: Generally, the variables defined at the program's top level are global variables, while the variables defined in the subroutine (generally a function) are generally local variables. If the variables have the same name, local variables work in local scopes, and vice versa.
When global variables are complex types (generally list, dictionary, and set), you can directly modify the global variables in the function.
When the global variable type is single (character, number, etc.), you can use global to redefine the global variable name in the function (this is generally not recommended, because you may not know how many times a function is called)
You can define a global variable (the name of the global variable) in a function. This is generally not recommended.