docs on for-loop with no __iter__?
Steven Bethard
steven.bethard at gmail.com
Sat Sep 4 19:36:02 EDT 2004
Andrew Dalke <adalke <at> mindspring.com> writes:
> Steven Bethard wrote:
> > Presumably there was a reason not to use len() to determine
> > the end of the sequence?
>
> Because that allows iteration over things where
> you don't yet know the size.
I'm trying to imagine a situation where it makes sense to want the x[i]
behavior from __getitem__ but where you don't know the final index. x[i]
suggests random access, so if you don't really have random access, shouldn't
you be defining __iter__ instead? Are there some good examples of classes
that allow the x[i] indexing but don't support random access (e.g. you can do
only do x[2] after you do x[1])?
(Obviously your FileReader class was an example of this, but I've assumed this
was just an example of how things *could* have been written, not how they
actually were. Please correct me if I'm wrong!)
Steve
More information about the Python-list
mailing list