itertools.flatten()? and copying generators/iterators.

Alex Martelli aleax at aleax.it
Tue Oct 28 09:28:02 EST 2003


Francis Avila wrote:
   ...
> Perhaps a guru can clarify the relationship between generators and
> iterators?

Sure: the use of "iterators" or "iterator objects" in the Python docs
does NOT mean "objects that are instance of <type 'iterator'>", it 
means "objects that satisfy the iterator protocol".

The situation may be a bit less confusing in today's Python, just
because the typenames involved have been changed a bit in a few
important special cases:

>>> iter(())
<tupleiterator object at 0x402deecc>

>>> iter([])
<listiterator object at 0x402dedec>

>>> iter('')
<iterator object at 0x402dee8c>

These types are unrelated -- each only inherits from object, as
you can check by viewing their __bases__ -- and besides the small
performance improvement that results from each of these iterator
objects 'knowing' the exact type of sequence it's iterating on,
the name diversification may help avoid the understandable kind
of confusion you may have been led into by older Python versions.


Alex





More information about the Python-list mailing list