生成器
只要在函数体内出现yield关键字,那么再执行函数就不会执行函数代码,会得到一个结果,该结果就是生成器。
生成器就是迭代器。
def foo():
	print('add')
	yield 1
	print('sidhi')
	yield 2
g=foo()
next(g)

res1=next(g)
print(res1)


yield的功能:
1.yield为我们提供了一种自定义迭代器对象的方法。
2.yield与retuen的区别,yield可以返回多次值,所以函数的暂停与继续的转态由yield帮我们保存的。


例:range的编写 #range(1,10,2)
def bar(x,y,z):
	while x <= y:
		yield x				 
		x+=z
g=bar(1,10,2)		
print(next(g))
			

案例:编写tail -f error.log |grep 'abc'
import time 
def foo(file):
	with open(file,'rb') as f:
		f.seek(0,2)  #光标直接到末尾
		while Ture:
			line=f.readline()  #.readline()每次只读一行
			if line:
				yield line
			else:
				time.sleep(0.03)	

def grep(line,pater):
for i in line:
	i=i.decode('utf-8')
	if pater in i:
		yield i

g=grep(foo('error.log'),'abc')
for i in g:
	print(i)

yiled表达式形式的用法:
def foo():
	print('%s is pig' %name)
	while Ture:
		bar=yield    #bar=yield='1111'
		print('%s is pig %s' %(name,bar))
	
g=foo('abc')
netx(g)  #初始化,也可以使用g.send(None)
然后g.send(),从代码执行到暂停的位置再将值传给yield ,与next一样。
g.send('1111')
g.send('2222')


面向过程编程:  过程指的是解决问题的步骤,即先设计框架,基于该思路编写程序,是一种机械式方式。
优点:复杂问题流程化,进而简单化。
缺点:可扩展性差

案例:编写grep -rl 'python' /etc
import os  
def init(funce):
	def titi(*args,**kawrgs):
		g=funce(*args,**kawrgs)
		next(g)
		return g
	return titi
@init #foo=titi
         
def foo(targrt):
	while Ture:
		path_a=yield
		g=os.wal(path_a)  #绝对路径模块
		for a,b,c in g:
			for c1 in c:
				abc_path=r'%s/%s ' %s(a,c)
				targrt.send(abc_path)
@init 	
def opener(targrt):
	while Ture:
		abc_path=yield
		with open(abc_path,'rb') as f:
			targrt.send(abc_path,f)

@init 
def cat(targrt):
	while Ture:
		abc_path,f=yield
		for line in f:
			res=targrt.send((abc_path,line))
			if res:
			      break

	
@init
def grep(targrt,pate):
	pate=pate.encode('utf-8')
	res=False
	while Ture:
		abc_path,line=yield res
		res=False 
		if pate	 in line:
			res=Ture
			targrt.send(abc_path)	


@init
def printer():
	while Ture:
		abc_path=yield
		print('%s' %abc_path)

g=foo(opener(cat(grep(printer(),'python'))))
g.send(r'/home')