W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
你想定義一個生成器函數(shù),但是它會調用某個你想暴露給用戶使用的外部狀態(tài)值。
如果你想讓你的生成器暴露外部狀態(tài)給用戶,別忘了你可以簡單的將它實現(xiàn)為一個類,然后把生成器函數(shù)放到iter()方法中過去。比如:
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()
為了使用這個類,你可以將它當做是一個普通的生成器函數(shù)。然而,由于可以創(chuàng)建一個實例對象,于是你可以訪問內部屬性值,比如 history
屬性或者是 clear()
方法。代碼示例如下:
with open('somefile.txt') as f:
lines = linehistory(f)
for line in lines:
if 'python' in line:
for lineno, hline in lines.history:
print('{}:{}'.format(lineno, hline), end='')
關于生成器,很容易掉進函數(shù)無所不能的陷阱。如果生成器函數(shù)需要跟你的程序其他部分打交道的話(比如暴露屬性值,允許通過方法調用來控制等等),可能會導致你的代碼異常的復雜。如果是這種情況的話,可以考慮使用上面介紹的定義類的方式。在 __iter__()
方法中定義你的生成器不會改變你任何的算法邏輯。由于它是類的一部分,所以允許你定義各種屬性和方法來供用戶使用。
一個需要注意的小地方是,如果你在迭代操作時不使用for循環(huán)語句,那么你得先調用 iter()
函數(shù)。比如:
>>> f = open('somefile.txt')
>>> lines = linehistory(f)
>>> next(lines)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'linehistory' object is not an iterator
>>> # Call iter() first, then start iterating
>>> it = iter(lines)
>>> next(it)
'hello world\n'
>>> next(it)
'this is a test\n'
>>>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: