Parameters, local variables, global variables and anonymous functions of function application

Functions are organized, reusable code snippets that implement a single, or associated function. Function can improve th...

Functions are organized, reusable code snippets that implement a single, or associated function. Function can improve the modularity of application and the reuse rate of code.

1. Parameters

  • Formal and actual parameters:
    When defining a function, the variable name in parentheses is called a formal parameter, or "parameter"

    When a function is called, the variable name in parentheses is called the actual parameter, or "argument"

  • Key parameters
    When a function is called, the parameter usage pattern that specifies a parameter equal to the specified parameter is called a keyword parameter. Keyword parameters can be used to pass arguments in the order of parameters, and the system will confirm which parameter the arguments are passed to by the name of the parameter.

>>> def fun1(name,action): return (name+"-->"+action) >>> fun1(name="I",action="god") 'I-->god' >>> fun1(action="god",name="I") 'I-->god' #Even if the name is after the call, the output will not be affected
  • Default parameters
    In some cases, a program needs to specify a default value for one or more parameters when defining a function, so that when calling a function, it can omit the parameter value passed in for the parameter, and directly use the default value of the parameter.
>>> def fun2(name,words="Make money"): return (name+"-->"+words) >>> fun2(name="I, Qin Shihuang") 'I, Qin Shihuang-->Make money' #Directly called the contents of words >>> fun2(name="I, Qin Shihuang",words="To unify the world") 'I, Qin Shihuang-->To unify the world' #You can also change the default
  • Indefinite length parameter
    You may need a function that can handle more arguments than you originally declared. These parameters are called indefinite length parameters. Parameters with an asterisk * will be imported as tuples to store all unnamed variable parameters.
>>> def fun3(*name): return name >>> fun3("I will go to heaven",2233,"...") ('I will go to heaven', 2233, '...') #Parameter types can also be different

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2. Local and global variables

  • Local variable: a variable defined within a function is called a local variable. Its scope is within the function.
  • Global variable: a variable defined outside a function is called a global variable. Its scope is global.
    The so-called global variables and local variables are mainly for internal and external functions.
>>> x=5 #global variable >>> def fun4(): return x >>> fun4() #Can be called directly 5 >>> print(x) #No problem calling global variables 5
>>> def fun5(): z=10 return z >>> fun5() #Call function to get result 10 >>> print(z) #Error will be reported when calling local variables Traceback (most recent call last): File "<pyshell#31>", line 1, in <module> print(z) NameError: name 'z' is not defined

The appearance of global and nonlocal keywords can change the scope of variables

  • Global can change local variables into global variables.
>>> def fun5(): global z #Change z to global variable here z=10 return z >>> fun5() 10 >>> print(z) #No more calls 10
  • The nonlocal keyword modifies a variable to indicate that it is a local variable in the previous function. If the local variable does not exist in the previous function, the nonlocal position will be wrong.
>>> def fun6(): x=5 def fun7(): x+=x return x return fun7() >>> fun6() #Call error Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> fun6() File "<pyshell#29>", line 6, in fun6 return fun7() File "<pyshell#29>", line 4, in fun7 x+=x UnboundLocalError: local variable 'x' referenced before assignment
>>> def fun6(): x=5 def fun7(): nonlocal x #This is a local variable in fun6, which can be called (and replaced with global) x+=x return x return fun7() >>> fun6() 10
>>> def fun6(): a=5 def fun7(): nonlocal x #The outer function does not define x, and directly reports an error SyntaxError: no binding for nonlocal 'x' found

Note: the range of use is different. The global keyword can be used anywhere, including in the top-level functions and nested functions. Even if the variable has not been defined before, it can be directly used after global modification. The nonlocal keyword can only be used in nested functions, and the corresponding local variables are defined in the outer functions, otherwise an error will occur. (see example)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Anonymous function: lambda
The syntax of the lambda function contains only one statement, as follows: lambda [arg1 [,arg2 argn]]:expression (that is, the left side of the colon is the parameter, and the right side is the expression return value)

>>> s = lambda x,y: x+y >>> print(s(3,4)) 7

The advantage is that not only can you simplify your code, but you don't have to worry about naming your thinking.
~
~
~

The above code is self typing, thank you for correcting.

20 June 2020, 03:40 | Views: 2971

Add new comment

For adding a comment, please log in
or create account

0 comments