This article brings you an introduction to the usage of recursive calls of Python generators (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Generator
What is a generator: As long as the yield keyword appears in the function body, then the function code will not be executed when the function is executed, and a result will be obtained. The result is the generator
The generator is the iterator
The function of yield
yield provides us with a way to customize the iterator object
## The difference between #yield and return: 1. Yield can return multiple values 2. The pause and resumption of functions are saved by yield for us As long as you see the function If yield appears in it, then it is a generatorExample 1: As we mentioned above, if we see yield in the function, then it is a generator, and the generator is an iterator, Then when it comes to iteration The processor should think of the value method of xx.__next__()def test(): print('=====>1') yield 1 print('=====>2') yield 2 print('=====>3') yield 3 g = test() #就相当于一个容器 print(g.__next__()) print(g.__next__()) print(next(g))
def test(): print('=====>1') yield 1 print('=====>2') yield 2 print('=====>3') yield 3 g = test() for i in g: print(i)
Example 2: To call the result of test1 to test2, you need to use yield to customize a generator
def test1(): for i in range(10): yield i #把0~9变成生成器返回给函数test1 g = test1() #g是个生成器 def test2(g): for i in g: print(i) test2(g)
Example 3: Log error monitor
import time def tail(filepath): #定义一个查看文件的函数 with open(filepath, 'rb') as f: #打开形参为filepath rb是二进制读 f.seek(0,2) #把光标移动到末尾 while True: #循环监控日志 data = f.readline() #读取文件末尾 if data: #加入有数据就用yield返回 yield data else:# 否则就睡眠0.05秒 time.sleep(0.05) def grep(file, k): #定义过滤关键字函数 for i in tail(file): #循环生成器中的数据 if k in i.decode('utf-8'): #因为是用二进制读取方式,所以需要解码显示 print(i.decode('utf-8')) grep('a.txt', '500') #监控a.txt最新日志,并过滤500的错误代码
## Another usage of yield, assign valuedef test(name):
while True:
foot = yield
print('%s正在吃%s' % (name, foot))
e = test('轩轩') #e是生成器
next(e) #初始化,e.__next__()
# e.send(None) #初始化,与上一行二选一
e.send('饺子') #发送值传给foot
e.send('冰激凌') #发送值传给foot
Run result:
##Recursive callRecursive call:
In the process of calling a function, the function itself is called directly or indirectly, which is called recursive callThe two necessary stages of recursion:
1 recursion, 2 backtrackingExample: A, B, C, D, and E, 5 people eat buns. We want to know how many buns A ate, but A said it was better than B. Eat 2 more pieces. B said he ate 2 more pieces than C. C said he ate 2 more pieces than D. Ding said he ate 2 more pieces than E. E said he didn't eat it.
Then because he knew that E didn't eat it , so according to the answers of A, B, C and D, we know that A ate 8, so the process of going back and forth is recursion and backtrackingage(A) = age(B) 2 age(B) = age(C) 2age(C) = age(D) 2age(D) = age(E) 2age( E) = 0def num(n): if n == 1: return 0 return num(n-1) + 2res = num(5) print(res)
The above is the detailed content of Introduction to the usage of recursive calls of python generators (code example). For more information, please follow other related articles on the PHP Chinese website!