Why is there no __iter__() for lists strings and tuples?
Oren Tirosh
oren-py-l at hishome.net
Sun Dec 22 09:17:09 EST 2002
On Sat, Dec 21, 2002 at 11:43:31PM -0300, Walter Moreira wrote:
> Sometime ago I also thought that this was the equivalence with the 'for'
> loop. But if you do:
>
> >>> for i in [1,2,3]:
> ... raise StopIteration
> ...
> Traceback (most recent call last):
> File "<stdin>", line 2, in ?
> StopIteration
> >>>
>
> So, it is not completely equivalent. I expected the StopIteration to
> finalize the 'for' loop. On the other hand, if the exception is raised
> from the iterator, the loop ends. Why does this asymmetric behavior
> ocurr?
Internally, the tp_next slot does not actually use the StopIteration
exception. It just returns NULL (for performance). A real exception is
only used when tp_next is exposed to Python code as the .next() method.
Oren
More information about the Python-list
mailing list