function is a program logic to the structure or process of a programming method, detailed instructions please refer to the "Python Learning Manual", can not be explained in detail in time, hope forgive me!.
First, create a function
1.1 DEF statement
def function (args):
"Documentation"
Function_body_suite
1.2 declarations, definitions and parameters
A declaration defines a function name and a parameter (name), and does not define a body code block of functions
Define a function body code block
Parameters mainly include positional parameters, default parameters, non-keyword arguments (*args), keyword arguments (**kargs), and must follow this order when creating functions. A non-keyword parameter (*args), a keyword argument (**kargs) used in the CREATE function, collects parameters, and is used in the call to unpack the parameters. Remember the default mutable parameter trap.
1.3 Forward References
In Python, a function does not have a forward-referenced rule (who first defines no impact, or even a non-sequential nesting), only needs to be defined before the call
def foo ()
Print (' foo ')
Bar ()
def bar ()
Print (' Bar ')
Foo ()
1.4 Properties
Accessing and adding attributes through the period property identifier, the function's properties are related to the function object, and the scope of the function is independent
1.5 inline functions
Create another function inside the function body
1.6 Function Adorner
@decorator (Dec_opt_args)
def function (args):
"Doc"
Body_suite
#for Example
@g
@f
def foo ():
Pass
foo = g (f (foo))
1.7 Partial function
From Functools Import Partial
ADD1 = Partial (add, 1) # ADD1 (x) = Add (1, x)
Basetwo = partial (int,base=2) #将参数base = 2, fixed to the fixed parameter of the INT function.
1.8 Packet Closure function
If, in an intrinsic function, a reference is made to a variable that is in an outer scope (but not a global scope), then the intrinsic function is a closed packet and has memory.
1.9 Recursion
If the function contains a call to itself, the function is a recursive function
def factorial (n):
if n = = 1 or N = = 0:
Return 1
Else
Return (N*factorial (n-1))
1.10 Functional Programming
Lambda [arg1 [, arg2, ...]]:expr
A lambda expression returns a function object that can be called.
1.11 Built-in functions
Map (), filter (), reduce () all create an iterative context, similar to the for
Map (func, Seqs) iterates through each element in the sequence and processes through the Func function, returning a list of all the values
Filter (func, seqs) iterates through each element in the sequence, leaving the function returned as a Boolean true element, adding all the values to a list and returning
Reduce (func, Seqs) takes the first two elements of the sequence, passes in the two-tuple function func to get a single value, and then passes the value with the next element, again passing in the two-tuple function func, and processing the sequence one at a time until the end.
Introduction to the functions of [Python]