Iterators

Stephen Hansen apt.shansen at gmail.com
Fri Oct 16 12:39:29 EDT 2009


On Fri, Oct 16, 2009 at 9:24 AM, Harald Kraemer <harryk at freenet.de> wrote:

> Stephen Hansen wrote:
> Nothing 'dumb' or 'smart' about it: it is simply that a file object is
>
>>  already an iterator. Trying to create an iterator from an existing
>>> iterator
>>> in Python never duplicates the iterator.
>>>
>>> >>> f = open('somefile')
>>> >>> iter(f) is f
>>> True
>>>
>>>
>>>  Except it is, IMHO, dumb vs smart-- but that doesn't mean the person who
>> implemented it is dumb nor that its dumb to have such an implementation.
>>
>>
> I think, a better terminology would be iterable versus iterator. An
> iterable object can return an iterator (which is then used to iterate over
> the elements of the iterable), while the iterator is just ... an iterator :)
>
>
That's entirely true from a technical standpoint, but-- since you use them
both in the exact same way in code, and Python calls iter() when needed to
convert the iterable to the iterator, and since ever iterator is also an
iterable (since it has an __iter__ which just returns self)...

I don't think the distinction is entirely clear enough to users; on a couple
occasions I had to dig into a library's code to see which kind it was. Not
that this is a huge and difficult burden, mind you. It can be a source of
confusion which could be documented a bit better is all. I just always
explained it as 'smart' vs 'dumb' and people got it better then 'iterable'
vs 'iterator' :)

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091016/bc41eeca/attachment.html>


More information about the Python-list mailing list