Before argument:
Last time thinking: load and run multiple decorators analysis
def deco1(func1): #func1=warpper2
def wrapper1(*args,**kwargs):
print('正在运行===>deco1.wrapper1')
res1=func1(*args,**kwargs)
return res1
return wrapper1
def deco2(func2):#func2=warpper3
def wrapper2(*args,**kwargs):
print('正在运行===>deco2.wrapper2')
res2=func2(*args,**kwargs)
return res2
return wrapper2
def deco3(x):
def outter3(func3): #func3=index
def wrapper3(*args,**kwargs):
print('正在运行===>deco3.outter3.wrapper3')
res3=func3(*args,**kwargs)
return res3
return wrapper3
return outter3
@deco1
#@deco1 ==>warpper2=deco1(warpper2) ==>warpper2=warpper1
@deco2
#@deco2 ==>warpper3=deco2(warpper3) ==>warpper3=warpper2
@deco3(111)
#deco3(111)=outter3 @outter3==>index = outter3(index)==>index = warpper3
#装饰器的加载顺序,从下到上
#运行顺序,从上到下
def index(x,y):
print('from index %s:%s' %(x,y))
A, yield expression
1 yield expression basic usage
As long as the yield keyword appears in the function, calling this function will not be executed, it returns a generator (custom iterators), where you can pass values to yield, to accept this value by a variable in the function
def func(name):
while True:
print(f"我是{name}")
x = yield
print(f"我爱吃{x}")
g = func("hz")
#给函数传参hz调用函数,此时发现函数内有yield关键字,挂起函数,函数调用的返回值是生成器
g.__next__()#执行迭代器功能查找下一个值,运行函数至yield处
g.send("水果")#给yield传值
#注意:这不是yield的返回值,返回值在函数定义的时候就写好了
g.send("巧克力")#给yield传值
>>>我是hz
>>>我爱吃水果
>>>我是hz
>>>我爱吃巧克力
>>>我是hz
Two, three expressions
Ternary expressions with the code is a shorthand way
Code that runs when the code if condition else running condition is not satisfied when the conditions are met: syntax
x= 1
y =2
if x > y :
print(x)
else:
print(y)
>>>2
print(x if x>y else y)
>>>2
print(x) if x>y else print(y)
>>>2
Third, the formula
List of Formula 1
l = ["a","b","c2","adsa","z"]
#需求:把列表内长度大于1的值添加到新列表new_l中
#基础做法:
for i in l :
if len(i)>1:
new_l.append(i)
#列表生成式做法:
new_l = [i for i in l if len(i)>1]
2 Dictionary of formula
d = {"a":1,"b":2,"c":3}
#生成一个新的字典,key是d中的所有key,value为none
#基础做法:
new_d = {}
for i in d:
new_d[i] = None
#字典生成式做法:
new_d = {key:None for key in d }
Collection of formula 3
#生成一个含有数字1-9的集合
#基础做法:
set1 = set()
print(type(set1))
for i in range(1,10):
set1.add(i)
#集合生成式做法:
s ={i for i in range(1,10)}
4 generator expression
#生成一个生成器
g = (i for i in range(10) if i >3)
#此刻g内部一个值都没
Fourth, the recursive function
1 recursive definition
It is a special form of nested functions
In the process of calling a function calls itself directly or indirectly
#直接调用本身
def foo():
foo()
foo()
#间接调用本身
def f1():
f2()
def f2():
f1()
f1()
2 Detailed recursive
If we execute the above code will find pycharm recursion error after about 1000, because the recursion itself is a very dangerous act, if we do not do anything with him permanently recursive calls will go directly cause memory overflow.
#需求:生成一个含有1-10的列表
#for循环方式
l=[]
for i in range(1,11):
l.append(i)
#函数递归方式
def appen(n):
if n == 11:
return
else:
l.append(n)
n+=1
appen(n)
appen(1)
Two-stage recursive
Back: call down layer by layer
#运行递归函数的过程
def func ():
func()
func()
Recursive: to meet certain conditions of the end, the end of the recursive call, and then return one level