每天CookBook之Python-062
- 定义带特殊状态的生成器函数
from collections import deque
class linehistory:
def __init__(self, lines, histlen=3):
self.lines = lines
self.history = deque(maxlen=histlen)
def __iter__(self):
for lineno, line in enumerate(self.lines, 1):
self.history.append((lineno, line))
yield line
def clear(self):
self.history.clear()
with open('passwd.txt') as f:
lines = linehistory(f)
for line in lines:
if 'python' in line:
for lineno, hline in lines.history:
print('{}:{}'.format(lineno, hline))
f = open('passwd.txt')
lines = iter(f)
print(next(lines))
print(next(lines))
print(next(lines))
print(next(lines))
out
11:passwd
12:passwd
13:python3333
passwd
passwd
passwd
passwd