definition
Packaging for a specific function represents a function or behavior.
When the function block statements can repeat, repeat call
effect
Improve code reusability and maintainability of the code clear hierarchy
Defined Functions
grammar:
def function name (parameter):
Function body
Explanation
- def keyword, meaning "definition"
- the number of seconds the function name of the function body statement, naming the same variable
- parameter, this method requires the definition information provided by the caller
- function body, the function of the complete statement
The first statement of the function may be selectively used documentation string instructions for function
call function
Function name (arguments)
return value
Definition: the results of this method definition tells the caller
Syntax: return [expression]
Description:
- expression is the need to return results
- return without an expression equivalent to return None
- without a return statement, the function returns after the implementation None, equivalent to finally add a return None
Variable / indistinguishable when varying the type of parameter passing
Immutable type parameters are:
Number (integer, floating point, complex)
Boolean value bool
None null
String str
Tuple tuple
Fixed set frozenset
The variable type parameters are:
List list
Dictionary dict
Collection set
Byte data bytearray
Description parameter passing:
When immutable type data transfer parameters, the internal function does not alter the value of
Variable-type data transmission parameters, the internal function can change the value of the original data
Function Arguments
Argument transfer mode
Location parameter passing
Definition: real positions corresponding to the primary parameter involved in
Keyword parameter passing
Definition: The argument for the name corresponding to the parameter
Parameter defined manner
The default parameter
Use the default value so that the caller can selectively deliver the information you need, not choice: also known as default parameters
Location parameter
According to a position one-parameter
The asterisk parameter tuple:
Syntax: def function name (* tuple parameter name)
Action: Position collected excess mass participation, an unlimited number
Description: General named args, parameter list can only have a maximum of
Keyword parameter
By keyword corresponding to the name of mass participation
Double asterisk dictionary parameter:
Syntax: def function name (parameter name ** Dictionary)
Action: a keyword collection redundant transmission parameters, not the number
Description: General named kwargs, parameter list can only have a maximum of
The order of the parameters from left to right
Location parameter - the asterisk tuple parameter - named key parameter - parameter double asterisk dictionary