首页 > 代码库 > 13、生成器函数
13、生成器函数
一、生成器函数
#生成器函数:只要函数体包含yield关键字,该函数的执行结果就是生成器函数
#生成器就是迭代器
# def foo():
# return 1
# return 2
# return 3
# return 4
#
# res1=foo()
# print(res1)
#
# res2=foo()
# print(res2)
# def foo():
# print(‘first‘)
# yield 1
# print(‘second‘)
# yield 2
# print(‘third‘)
# yield 3
# print(‘fourth‘)
# yield 4
# print(‘fifth‘)
#
# g=foo()
# for i in g:
# print(i)
# print(g)
#
# print(next(g)) #触发迭代器g的执行,进而触发函数的执行
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# def counter(n):
# print(‘start...‘)
# i=0
# while i < n:
# yield i
# i+=1
# print(‘end...‘)
#
#
# g=counter(5)
# print(g)
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
‘‘‘
yield的功能:
1.相当于为函数封装好__iter__和__next__
2.return只能返回一次值,函数就终止了,
而yield能返回多次值,每次返回都会将函数暂停,下一次next会从
上一次暂停的位置继续执行
‘‘‘
二 、表达式形式的yield的用途
#tail -f a.txt | grep ‘python‘
import time
def tail(filepath):
with open(filepath,encoding=‘utf-8‘) as f:
f.seek(0,2)
while True:
line=f.readline().strip()
if line:
yield line
else:
time.sleep(0.2)
# t=tail(‘a.txt‘)
#
# for line in t:
# print(line)
def grep(pattern,lines):
for line in lines:
if pattern in line:
yield line
g=grep(‘python‘,tail(‘a.txt‘))
print(g)
for i in g:
print(i)
#grep -rl ‘python‘ /root
import os
def init(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
next(res)
return res
return wrapper
@init
def search(target):
while True:
search_path=yield
g=os.walk(search_path)
for par_dir,_,files in g:
for file in files:
file_abs_path=r‘%s\%s‘ %(par_dir,file)
# print(file_abs_path)
target.send(file_abs_path)
@init
def opener(target):
while True:
file_abs_path=yield
# print(‘opener func==>‘,file_abs_path)
with open(file_abs_path,encoding=‘utf-8‘) as f:
target.send((file_abs_path,f))
@init
def cat(target):
while True:
file_abs_path,f=yield #(file_abs_path,f)
for line in f:
tag=target.send((file_abs_path,line))
if tag:
break
@init
def grep(target,pattern):
tag=False
while True:
file_abs_path,line=yield tag
tag=False
if pattern in line:
tag=True
target.send(file_abs_path)
@init
def printer():
while True:
file_abs_path=yield
print(file_abs_path)
x=r‘C:\Users\Administrator\PycharmProjects\python17期\day5\a‘
g=search(opener(cat(grep(printer(),‘python‘))))
print(g)
g.send(x)
‘‘‘
面向过程的程序设计:是一种流水线式的编程思路,是机械式
优点:
程序的结构清晰,可以把复杂的问题简单
缺点:
1 扩展性差
应用场景:
1 linux内核,git,httpd
‘‘‘
三、yield的另外一种用法
#yield的语句形式: yield 1
#yield的表达式形式: x=yield
#协程函数
def deco(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
next(res)
return res
return wrapper
@deco
def eater(name):
print(‘%s ready to eat‘ %name)
food_list=[]
while True:
food=yield food_list
food_list.append(food)
print(‘%s start to eat %s‘ %(name,food))
g=eater(‘alex‘)
# print(g)
# next(g) #等同于 g.send(None)
#
# g.send(‘手指头‘)
# g.send(‘脚指头‘)
# g.send(‘别人的手指头‘)
# g.send(‘别人的脚指头‘)
# print(g)
print(g.send(‘脚趾头1‘))
print(g.send(‘脚趾头2‘))
print(g.send(‘脚趾头3‘))
#x=yield
#g.send(‘1111‘),先把1111传给yield,由yield赋值给x
# 然后再往下执行,直到再次碰到yield,然后把yield后的返回值返回
13、生成器函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。