Fifteen (Functions Overview) python learning Notes

Function : In one project, some functions require repeated use, it functions as a function of the package. When it is desired to use this function directly calls functions
essentially : encapsulation function is the function of
advantages : (1) to simplify the structure of the code, the code multiplicity increases (the degree of repeated use)
(2) To modify certain debugging function or a BUG, only you need to modify the function corresponding to
the definition of the function 1.
format: def function name (parameter list):
(space) statement
(space) return expression
def: def function block to start with keywords
function name: follow the rules for identifiers
(): beginning and end of the argument list of
list of parameters (parameter 1, parameter 2, ......, parameter n): any of the parameters and variables passed into the function, it must be placed in parentheses between, separated by commas. Is a function of the information from the caller of the function where the acquisition of
the colon: a function of the content (encapsulated functionality) from the beginning of the colon and indentation
statement: function package functions
return: generally used for the end of the function, and return information to the caller of the function
expression type: to return to the caller of the function information
note: the final return expression can not write, equivalent to return None

2. The function call
format: function name (parameter list)
the function name: the name of the function you want to use
the list of parameters: function caller information passed to the function. If there is no parameter, the parentheses can not omit
the nature of the function call: argument to a procedure parameter assignment
3. function parameter
arguments: actual arguments passed to the function data when calling a function, essentially a value
parameter: form parameters, variables in parentheses defined function is essentially a variable
Note: this parameter must be in order, the number corresponding to the current to
the return value of the function 4.
executing the return statement of the function is finished, execution code does not return back
the transfer parameters
(1) the value of transmission: passing immutable type (string, tuple, number are immutable)
(2) pass by reference: variable transmission of the type (list, dict, set variable)
6. keyword parameters
keyword parameters: allow inconsistent with the order of the parameters defined in the function call
Here Insert Picture Description
7. the default parameters
concepts: the function is called, if no parameter, the default parameters
if use default parameters, preferably the default parameters into Finally,
Here Insert Picture Description
Here Insert Picture Description
8. variable length parameter
concept: to handle more than defined parameters
plus an asterisk (*) variables to store all variable parameters are not named in the letter if No argument is given, then it is an empty tuple number of calls
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
9. anonymous function
concept: do not use a statement such as def defined function that uses lambda to create an anonymous function
Features:
(. 1) only a lambda expression, function body simpler than DEF
(2) the body is a lambda expression, rather than block, only simple logic can only be encapsulated in lambda expressions
(. 3) has its own function named lambda space, and can not be accessed outside its own list of parameters or global namespace parameter
(4) although is a lambda expression and it seems only write a single line, with C and C ++ inline functions in different
formats:
lambda parameter 1, parameter 2, ..., parameter n: expression
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_38324954/article/details/95202883