day5:协成函数与import、for...import...的使用
一、协程函数
1、把函数的执行结果封装好__iter__和__next__,即得到一个迭代器
2、与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yield可以返回多次值
3、函数暂停与再继续运行的状态是有yield保存
# def func(count): # print('start') # while True: # yield count # count+=1 # # g=func(10) # # print(g) # print(next(g)) # # print(next(g))
yield的表达式形式的应用
# def eater(name): # print('%s 说:我开动啦' %name) # while True: # food=yield # print('%s eat %s' %(name,food)) # # alex_g=eater('alex') # print(alex_g) # print(next(alex_g)) # print('==============>') # print(next(alex_g)) # print('==============>') # print(next(alex_g))
用法:
# def eater(name): # print('%s 说:我开动啦' %name) # food_list=[] # while True: # food=yield food_list # food_list.append(food) #['骨头','菜汤'] # print('%s eat %s' %(name,food)) # # alex_g=eater('alex') # #第一阶段:初始化 # next(alex_g) #等同于alex_g.send(None) # print('===========>') # # #第二阶段:给yield传值 # print(alex_g.send('骨头')) #1 先给当前暂停位置的yield传骨头 2 继续往下执行,直到再次碰到yield,然后暂停并且把yield后的返回值当做本次调用的返回值 # # print('===========>') # print(alex_g.send('菜汤')) # print(alex_g.send('狗肉包子'))
# def eater(name): # print('%s 说:我开动啦' %name) # food_list=[] # while True: # food=yield food_list # food_list.append(food) #['骨头','菜汤'] # print('%s eat %s' %(name,food)) # # # def producer(): # alex_g=eater('alex') # #第一阶段:初始化 # next(alex_g) # #第二阶段:给yield传值 # while True: # food=input('>>: ').strip() # if not food:continue # print(alex_g.send(food)) # # # producer() #解决初始化问题 def init(func): def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper @init def eater(name): print('%s 说:我开动啦' %name) food_list=[] while True: food=yield food_list food_list.append(food) #['骨头','菜汤'] print('%s eat %s' %(name,food)) alex_g=eater('alex') #第二阶段:给yield传值 # print(alex_g.send('骨头')) #1 先给当前暂停位置的yield传骨头 2 继续往下执行,直到再次碰到yield,然后暂停并且把yield后的返回值当做本次调用的返回值 # print('===========>')
二、递归
递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身
直接
# def func(): # print('from func') # func() # # func() #间接 # def foo(): # print('from foo') # bar() # # def bar(): # print('from bar') # foo() # # foo()
# age(5)=age(4)+2 # age(4)=age(3)+2 # age(3)=age(2)+2 # age(2)=age(1)+2 # age(1)=18 # age(n)=age(n-1)+2 #n>1 # age(1)=18 #n=1 # def age(n): # if n == 1: # return 18 # return age(n-1)+2 # # print(age(5))
递归的执行分为两个阶段:
1 递推
2 回溯
# l =[1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15,[16,[17,]],19]]]]]]] # # def search(l): # for item in l: # if type(item) is list: # search(item) # else: # print(item) # # search(l) #
三、二分法
# l = [1,2,5,7,10,31,44,47,56,99,102,130,240] # # # def binary_search(l,num): # print(l) #[10, 31] # if len(l) > 1: # mid_index=len(l)//2 #1 # if num > l[mid_index]: # #in the right # l=l[mid_index:] #l=[31] # binary_search(l,num) # elif num < l[mid_index]: # #in the left # l=l[:mid_index] # binary_search(l,num) # else: # print('find it') # else: # if l[0] == num: # print('find it') # else: # print('not exist') # return # # binary_search(l,32)
#二分法 l = [1,2,5,7,10,31,44,47,56,99,102,130,240] def binary_search(l,num): print(l) if len(l) == 1: if l[0] == num: print('find it') else: print('not exists') return mid_index=len(l)//2 mid_value=l[mid_index] if num == mid_value: print('find it') return if num > mid_value: l=l[mid_index:] if num < mid_value: l=l[:mid_index] binary_search(l,num) binary_search(l,32)
四、import的用法
导入模块干了哪些事:
1 执行源文件
2 以一个源文件的全局名称空间
3 在当前位置拿到一个模块名,指向2创建的名称空间
# import spam # money=100000000000 # def read1(): # print('from test') # # print(spam.money) # # print(spam.read1) # # spam.read1() # # # spam.read2() # spam.change() # print(money) # spam.read1() # # import spam as s1 # print(s1.money)
# sql_type=input('sql_type: ') # if sql_type == 'mysql': # import mysql as sql # # elif sql_type == 'oracle': # import oracle as sql # # sql.sqlparse() import sys print(sys) import spam print(spam)
五、for...import的用法
优点:使用源文件内的名字时无需加前缀,使用方便
缺点:容易与当前文件的名称空间内的名字混淆
# from spam import money,read1,read2,change # money=0 # print(money) # print(read1) # # read1() # def read1():print('ok') # read2() # # money=10 # change() # print(money) # from spam import money as m # # print(m) from spam import * # print(_money) # read1() # print(read2) print(money) print(x) print(read1)