我遇到一个示例,将yield
用作陷阱
def foo():
for i in range(10):
print("I'm foo")
yield
测试并输出
In [164]: g = foo()
In [165]: next(g)
I'm foo
In [166]: next(g)
I'm foo
yield
是yield None
的缩写吗?
其他建议问题。
打个比方,我可以将generator
作为类,将g = generator()作为实例吗?