Overview:
- The __next__() function of the generator iterator starts/resumes a generator function.
- The __next__() method resumes the current yield statment and returns the value of the next yield.
- The __next__() method is used by the built-in function next() as well as the for loops.
Example:
# Example python program that uses __next__(), next # Create a generator iterator # Use the __next__() method # Get the value through the built-in function next() # Now use a for loop # Try getting a value after the generator is exhaust |
Output:
Received value using __next__():2 Received value using next():4 Received value using for:6 Received value using for:8 Received value using for:10 Traceback (most recent call last): File "/Users/vinodh/pprogs/gennext1.py", line 25, in <module> receivedValue = geny.__next__() StopIteration |