K
Krishnan Shankar
Hi Friends,
I am new to Generators and was learning the same by experimenting. I wrote
a small code which is as below.
.... print "The number is", var
.... if var % 2 == 0:
.... yield var
.... else:
.... print "Number is odd"
....
But when i was executing i saw a behavior i could not understand. When i
gave an even number,
1. The generator object was created
2. When i gave next the argument was yielded.
3. In the subsequent next the Stop Iteration was raised.
The number is 78
78Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
But When i ran with an odd number the result of "Number is odd" is printed.
But it seems the generator runs again by itself to Stop Iteration.
The number is 77
Number is odd
Traceback (most recent call last):
How did this happen automatically? I am not able to get the execution of a
generator. Can someone please help me in understanding?
Regards,
Krishnan
I am new to Generators and was learning the same by experimenting. I wrote
a small code which is as below.
.... print "The number is", var
.... if var % 2 == 0:
.... yield var
.... else:
.... print "Number is odd"
....
But when i was executing i saw a behavior i could not understand. When i
gave an even number,
1. The generator object was created
2. When i gave next the argument was yielded.
3. In the subsequent next the Stop Iteration was raised.
The number is 78
78Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
But When i ran with an odd number the result of "Number is odd" is printed.
But it seems the generator runs again by itself to Stop Iteration.
The number is 77
Number is odd
Traceback (most recent call last):
How did this happen automatically? I am not able to get the execution of a
generator. Can someone please help me in understanding?
Regards,
Krishnan