1

I'm not sure what's happening here. Today is my first foray into the world of generators. I put this into the pythontutor visualizer but I'm not understanding why this is happening. The visualizer spits out "generator return instance". I've read the other SO threads that are similar to mine though I'm unfortunately not understanding why this is. In addition to this specific issue I'd greatly appreciate any thoughts on great ways to learn to use generators correctly and efficiently. Thank you!

def even(nums):
    for number in nums:
        if number % 2 == 0:
             yield number


def find_evens(number_list):
    return even(number_list)

>>> find_evens([1,2,3,4,5,6])
<generator object even at 0x104f7af10>
1

3 Answers 3

2

The Python REPL is just giving your back the default representation of the generator object.

A generator won't yield it's values on its own, you'll have to force it to. Many options exist to force, for example wrapping it in a list:

list(find_evens([1,2,3,4,5,6]))
[2, 4, 6]

Or, similarly, by iterating through it:

for i in find_evens([1, 2, 3, 4, 5, 6]):
    print(i)

Both these examples (looping, calling list) will call __next__ on the generator object forcing it to return the next value according to the statements you've written.

As for a bit more information on these, you could always take a look at the Python wiki Page on Generator.

2

A generator object has the method __next__().

This method must be called (explicitly or implicitly) to obtain its next value.

(It is called implicitly and repeatedly until the exhaustion by using the generator object in some contexts - as in for loop or list() function.)

Once a generator object is created:

  • the 1st  use of the __next__() method returns the value of the 1st  yield statement,
  • the 2nd use of the __next__() method returns the value of the 2nd yield statement,
  • .......
  • and so on - until there is no additional yield statement, in which case the generator object is exhausted (and useless) as it from this moment gives - instead of the next value - only the exception StopIteration.

Compare

>>> even([1, 2, 3, 4, 5, 6]).__next__()
2
>>> even([1, 2, 3, 4, 5, 6]).__next__()
2
>>> even([1, 2, 3, 4, 5, 6]).__next__()
2

with

>>> gen = even([1, 2, 3, 4, 5, 6])
>>>
>>> gen.__next__()
2
>>> gen.__next__()
4
>>> gen.__next__()
6
>>> gen.__next__()
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    gen.__next__()
StopIteration
>>>
>>> gen.__next__()
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    gen.__next__()
StopIteration

In the first case a generator is created again and again, while in the second one it is created once and then used repeatedly until its exhaustion (and 1 more time - only for the illustration).

0

You can imagine that you only get a pointer to where a generator is located. By printing it, you print the location where it is. But to actually get the values, you have to say give me the values, and you do that either by doing list or using it in a for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.