string: __iter__()?

mrquantum mrquantum at holon.at
Wed Oct 4 06:59:05 EDT 2006


Am Wed, 04 Oct 2006 12:24:48 +0200 schrieb Peter Otten:

> 
> The older pre-__iter__() iteration style relying on __getitem__() still
> works:
> 
>>>> class A:
> ...     def __getitem__(self, index):
> ...             return [3,2,1][index]
> ...
>>>> for item in A():
> ...     print item
> ...
> 3
> 2
> 1
> 

Thanks! I see:

>>> class B:
...   def __iter__(self):
...     for i in xrange(3):
...       yield [3,2,1][i]
...
>>> myIter = B().__iter__()
>>> myIter.next()
3
>>> myIter.next()
2
>>> myIter.next()
1

Chris



More information about the Python-list mailing list