I was trying to understand generators in Python and implemented this:
def yfun():
print("into y fun ... ")
for x in range(1,6):
print("tryin to yield : {}".format(x))
yield x
yieldVar = yfun()
for val in yieldVar:
print("value generated -> ", val)
The output comes as below:
into y fun ...
tryin to yield : 1
value generated -> 1
tryin to yield : 2
value generated -> 2
tryin to yield : 3
value generated -> 3
tryin to yield : 4
value generated -> 4
tryin to yield : 5
value generated -> 5
Can you please explain why is the function being called again and again ? Isn't the variable supposed to have the values and iteration should be done on generator variable ?