In general, a function is a well-organized, reusable piece of code that has some functionality. Functions can improve the modularity of the application and the reuse of the code, and in Python there are many built-in functions, such as print (), and Python allows the user to customize the function.
This article summarizes the function usage of Python3 in the following example:
First, the definition
The definition function uses the keyword DEF, followed by the name of the function, and the optional argument list in parentheses (), which begins with a colon and indents. The general format is as follows:
def function name (argument list): "" " Document String" "" function body
Note: The parameter list is optional, the document string is optional, and the return statement is optional.
Example:
def fib (n): "" " Print a Fibonacci series" "" A, b = 0, 1 while b < n: Print (b, end= ") A, B = B, a+b print () fib (#) # call F = fib
The value of a function name is a type of user-defined function. The value of a function name can be given another name so that it can also be used as a function.
Second, the function variable scope
A variable defined inside a function has a local scope and has a global scope defined outside the function. Note: You can reference a global variable inside a function, but you cannot assign a value to it (unless you declare it with global).
A = 5 # global variable a def func1 (): print (' Func1 () print a = ', a) def func2 (): a = # local variable a print (' Fu NC2 () Print a = ', a) def func3 (): Global a = ten # Modify the globals variable a print (' func3 () print a = ', a)
Third, function call
1. Normal call
As with function calls in other languages, in Python, when you call a function, you need to give the same number of arguments as the parameter and correspond in order one by one.
def fun (name, age, gender): print (' name: ', Name, ' Age: ', age, ' Gender: ', gender,end= ') print ()
2. Calling functions using keyword parameters
Functions can also be called by keyword=value keyword parameters, because we explicitly point out the correspondence, so the order of the parameters is irrelevant.
def fun (name, age, gender): print (' name: ', Name, ' Age: ', age, ' Gender: ', gender,end= ') print ()
3. Call the function with default argument
A function in Python can also specify a default value for one or more parameters so that it can be selectively omitted when called:
def fun (A, B, c=5): print (A+B+C)
Note: Typically, the default value is computed only once, but if the default value is a Mutable object when it differs, such as when a list, dictionary, or most class object is used. For example, the following function accumulates parameter values in subsequent calls :
def fun (A, l=[]): l.append (a) print (L)
4. Call variable parameter function
Specify that a function can receive any number of arguments by adding an asterisk (*) or two asterisk (* *) before the parameter.
def fun (*args): Print (type (args)) print (args) fun (1,2,3,4,5,6) # output: #
# (1, 2, 3, 4, 5, 6) def fun (**args): print (Type (args)) print (args) fun (a=1,b=2,c=3,d=4,e=5) # output: #
# {' d ': 4, ' E ': 5, ' B ': 2, ' C ': 3, ' a ': 1}
As can be seen from the output of two examples: when a parameter is shaped like *args, any actual attendees passed to the function are wrapped in a tuple (tuple) by position, and any key=value that are passed to the function are wrapped into a dictionary (dict) when the parameter is shaped like **args.
5. Calling functions by unpacking parameters
The last point is that when you pass any number of arguments, they are packaged into a tuple or dictionary, and of course there is an unpacking (unpacking) package. Unpack list, tuple, and dictionary by single and double star:
def fun (A=1, b=2, c=3): print (a+b+c) fun () # Normal call to List1 = [one, all,] Dict1 = {' A ': +, ' B ': +, ' C ': $ Fun (*l IST1) # Unpacking list Fun (**dict1) # Unpacking Dictionary
NOTE: * Used for unpacking sequence,** for unpacking dictionaries. The unpack dictionary will get a series of key=value, so it is essentially a function called using the keyword arguments.
Iv. lambda expression
Lambda keywords can create small anonymous functions. A lambda function can receive any number of arguments but can only return the value of an expression, which has the following general form:
Lambda expressions can be used wherever function objects are needed, and they are syntactically limited to a single expression:
f = lambda x, y:x+y print (f (Ten)) def make_fun (n): return lambda x:x+n
V. Document string
The first statement of a function body can be a string enclosed in three quotation marks, which is the document string of functions, or docstring. We can use print (function.__doc__) to output the document:
def fun (): "" "Some information of the This function. This is documentation string. "" Return
the document string is used primarily to describe some information about a function that allows the user to interactively browse and output . It is recommended that you develop a good habit of adding document strings to your code.