who uses Python or Ruby, and for what?
Fredrik Lundh
fredrik at pythonware.com
Thu May 3 13:43:10 EDT 2001
Thomas Bellman wrote:
> > the iterator is the object that responds to __getitem__(lastindex+1),
> > not the loop variable itself.
>
> I think you should reread the section on the Iterator pattern in
> "Design Patterns". To follow the iterator pattern, the iterator
> should be a *separate* object from the sequence that you are
> iterating over.
okay, I'll try again:
the iterator is the object that responds to __getitem__(lastindex+1)
by fetching the next element, not the loop variable itself.
(yeah, you can misinterpret that one too, but if you do that on
purpose, I'll come over and slap you with a huge big fish)
> FileInput is an iterator, yes. As is the objects returned by
> xrange() (which iterates over the integral numbers) and the
> .xreadlines() method of file objects
and PIL's ImageSequence.Iterator, the DirectoryWalker sample I've
posted a couple of times, etc. iterators are everywhere.
> The normal iteration API of old Python (old as in version 2.1 or
> older, of course :-), i.e putting a __getitem__() method on the
> collection type/class, *isn't* the Iterator pattern.
not by itself, no. but you can use __getitem__ to implement
iterators, as we both know. no disagreement here.
> "I don't think [that word] means what you
> think it means." -- The Princess Bride
if [that word] is "iterator", I don't think that quote is relevant
to this subthread ;-)
Cheers /F
More information about the Python-list
mailing list