need help on need help on generator...

Terry Reedy tjreedy at
Sat Jan 22 18:36:12 EST 2005

"Francis Girard" <francis.girard at> wrote in message 
news:200501221106.49241.francis.girard at

>If I understand correctly,


> a "generator" produce something over which you can
> iterate with the help of an "iterator".

To be exact, the producer is a generator function, a function whose body 
contains 'yield'.  In CPython, the difference after executing the def is 
that a generator function has a particular flag set.  People sometimes 
shorten 'generator function' to 'generator' as you did, but calling both a 
factory and its products by the same name is confusing.  (For instance, try 
calling an automobile factory an automobile).

>>> def genf(): yield 1
>>> genf
<function genf at 0x008873B8>

The result of calling a generator function is a generator, which is one but 
only one type of iterator.

>>> gen = genf()
>>> gen
<generator object at 0x008781F8>
>>> dir(gen)
[<stuff inherited from object>, '__iter__',  ' gi_frame', 'gi_running', 

The .__iter__ and .next methods make this an iterator.  The two data 
attributes are for internal use.

> Can you iterate (in the strict sense
>of an "iterator") over something not generated by a "generator" ?

Of course.  Again, a generator is one specific type of iterator, where an 
iterator is anything with the appropriate .__iter__ and .next methods.

Terry J. Reedy

More information about the Python-list mailing list