如何判断是否使用了生成器?即:
def code_reader(code):
for c in code:
yield c
code_rdr = code_reader(my_code)
a = code_rdr.next()
foo(code_rdr)
在调用
foo
之后,我想知道code_rdr
上是否由foo
调用了.next()
。
当然,我可以通过一些带有next()
调用计数器的类来包装它。
有没有简单的方法来实现这个功能?inspect.getgeneratorstate()
函数。因此,您可以简单地使用inspect.getgeneratorstate(gen) == 'GEN_CREATED'
:>>> import inspect
>>> gen = (i for i in range(3))
>>> inspect.getgeneratorstate(gen)
'GEN_CREATED'
>>> next(gen)
0
>>> inspect.getgeneratorstate(gen)
'GEN_SUSPENDED'
next()
已经被调用了? - Chris_Randsfoo
前后 inspect.getgeneratorlocals()
的 return
值,但并不是所有情况都适用(例如如果你的生成器是 (c for c in 'aaa')
)。无论如何,我建议你将其作为一个新问题提出,并使用不同的标题等,这是一个相当不同的问题,这样你会得到更多的关注。 - Chris_Rands我已经使用了附加的可能答案中的想法,下面重新定义了code_reader
函数:
def code_reader(code):
length = len(code)
i = 0
while i < length:
val = (yield i)
if val != 'position':
yield code[i]
i += 1
a = code_reader("foobar")
print a.next()
print a.send('position')
print a.next()
print a.send('position')
print a.send('position')
print a.next()
print a.send('position')
输出:
0
0
f
1
1
o
2
foo
返回的项目索引怎么样,例如code.index(foo(code_rdr))
?我知道,对于重复项来说不是完美的,但这是一个选项。 - Ma0foo
很可能是一个黑盒... - Willem Van Onsem