For the below code
# A simple generator function
def infinite_sequence():
num = 1
while True:
yield num
num += 1
aaa = infinite_sequence()
bbb = infinite_sequence()
ccc = infinite_sequence()
print(next(aaa))
print(next(aaa))
print(next(bbb))
print(next(bbb))
print(next(ccc))
print(next(ccc))
the output is: 1 2 1 2 1 2
When trying to call the same generator functions from another function, the output is not as expected
def switchAction(input):
action_item = {
"A": next(aaa),
"B": next(bbb),
"C": next(ccc)
}
return action_item.get(input)
print(switchAction("A"))
print(switchAction("A"))
print(switchAction("B"))
print(switchAction("B"))
print(switchAction("C"))
print(switchAction("C"))
The output is: 1 2 3 4 5 6
Why the counter is continuing across generator functions in the case of calling from another function? How to achieve the same output as that of first case for the second case above.