table of Contents
- Defined functions
- Three methods defined functions
- Function return value
- Function call
- Function parameters
- Vararg
- Function object
- Nested functions
- Namespace and scope
- Closure function
- Decorator
- Iterator
- A triplet of expressions
- List comprehensions
- Dictionary of formula
- Generator expressions
- Builder
- Anonymous function
- Recursion
- Built-in functions
- Process-oriented programming
Defined functions
def 函数名():
code
return 返回值
Three methods defined functions
- There are function parameters
- No-argument function
- Empty function
Function return value
- return termination function
- The return value can return multiple values, stored in a tuple
- The default return None
Nonetype ....
Function call
Function name()
Function parameters
Katachisan
Description significance
Location parameter
One by one from left to right
The default parameter
Default values, the default parameter must be placed behind the position parameter
Arguments
It has a specific value
Argument position
A left to right pass a value to the parameter
Keyword argument
In the form of key-value pairs transmitted parameter; Keyword argument must be placed behind the position of the argument
Vararg
*
Katachisan
Receiving extra argument position, stored in the form of a tuple
Arguments
Broken tuple argument in the position to form a reference mass parameter
**
Katachisan
Keyword receiving extra arguments, stored as dictionary
Arguments
**{'a':1} --> a=1
Break up the dictionary, in the form of keyword arguments passed to the parameter (parameter have to have the parameter name key)
Function object
- Quote
- Function parameters
- Function's return value
- Container earth element
Function object variable name is the function name ==
Nested functions
def f1():
def f2():
pass
Namespace and scope
- Internal
- Overall situation
- Partial
Order of execution: Built -> Global -> partial
Search order: current -> Local -> Global -> Built-in
Global scope:
Local scope
- (X = 3) unrelated global scope (x = 1) and the local scope
- Local scope (x = 1) and the local effects of 1 (x = 3) and unrelated 2
Closure function
A variable x and the function within the function package B, then the function returns the value returned by the object A function of B
def B(x):
# x = 1
def A():
print(x)
pass
return A
Decorator
Function is used to add functionality, he is also a function of the nature of
- Without changing the source code of the function to be decorated
- It does not change the decorated function is called
def outter(func):
def wrapper(*args,**kwrags):
# 逻辑
res = func(*args,**kwargs) # func是被装饰的函数
return res
return wrapper
@outter
def index():
pass
def sanceng(engine):
def outter(func):
def wrapper(*args,**kwrags):
# 逻辑
res = func(*args,**kwargs) # func是被装饰的函数
return res
return wrapper
return outter
@sanceng(engine)
def index():
pass
Iterator
Iterables: iter objects have methods, the object may not necessarily be an iterative iterator object
Iterator object: iter and next object has methods, iterator object must be iterables, plus iter method iterator object itself or iterator
for circulation principle
for i in lt:
- The lt iterator object into
- Then use the next iteration method to get each element
- Capture aborted while loop
A triplet of expressions
print(1) if i > 10 else print(2)
List comprehensions
[I for i in range (10)] # 0-9 has come out
Dictionary of formula
{k:v for k,v in dic.items()}
Generator expressions
(I for i in range (10)) # next value not used, there is no value
Builder
Custom iterator
def func():
yield
yield:
1. 暂停函数,遇到下一个yield继续运行函数体代码
- It has a return value
- Let function () becomes a generator object
return:
1. 终止函数
- return value
Anonymous function
Function without a name parameter lambda: block
Generally not used alone, with max / min / map / filter / sorted MS
Recursion
It calls the function itself, but have to have a termination condition
Built-in functions
Is the direct use of built-in functions, the python interpreter belonging to the
Data type of built-in functions to use only the data type itself
enumerate()
Process-oriented programming
assembly line
Advantages: clear thinking
Disadvantages: poor scalability; not independent of function and function; trouble debugging