How to use generators in Python?
Look at this example, pay attention to the yield statement:
class Experiment:
def __init__(self):
self.q = [1, 2, 3, 4, 5]
self.lent = len(self.q)
def func_uses_generator(self):
for tt in range(0, self.lent):
x = self.q[tt]
yield x
ob = Experiment()
for ind in ob.func_uses_generator():
print(ind)
output
1
2
3
4
5
Comments
Post a Comment