[Python-Dev] The iterator story
Aahz
aahz@pythoncraft.com
Sat, 20 Jul 2002 10:09:23 -0400
On Sat, Jul 20, 2002, Oren Tirosh wrote:
> On Fri, Jul 19, 2002 at 04:22:26PM -0700, Neil Schemenauer wrote:
>> Neal Norwitz wrote:
>>>
>>> In what context? Were you iterating over a file or something else?
>>> I'm wondering if this is a problem, perhaps pychecker could generate
>>> a warning?
>>
>> I was switching between implementing something as a generator and
>> returning a list. I was curious why I was getting different behavior
>> until I realized I was iterating over the result twice. I don't
>> think pychecker could warn about such a bug.
>
> That's the scenario that bit me too. For me it was a little more difficult
> to find because it was wrapped in a few layers of chained transformations.
> I can't tell by the last element in the chain whether the first one is
> re-iterable or not.
>
> My suggestion (which was rejected by Guido) was to raise an error when an
> iterator's .next() method is called afer it raises StopIteration. This
> way, if I try to iterate over the result again at least I'll get and error
> like "IteratorExhaustedError" instead something that is indistinguishable
> from an iterator of an empty container. I hate silent errors.
I'm still not understanding how this would help. When a chainable
transformer gets StopIteration, it should immediately return. What else
do you want to do?
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
Project Vote Smart: http://www.vote-smart.org/