Strange behaviour with reversed()
Duncan Booth
duncan.booth at invalid.invalid
Fri Oct 19 16:49:01 EDT 2007
Andreas Kraemer <akraemer at sbcglobal.net> wrote:
>> The only other behaviours I would regard as intuitive for iteration over
>> a mutating sequence would be to throw an exception either for mutating
>> the sequence while the iterator exists or for using the iterator after a
>> mutation.
>
> Maybe it would have been slightly more intuitive if reversed() had
> been implemented like this,
>
> def Reversed(seq):
> for i in xrange(len(seq)-1,-1,-1):
> yield seq[i]
>
> so that the length of the sequence is determined when the iteration
> starts, not when the iterator is created?
Perhaps, but either way it comes down to "don't modify the sequence while
iterating".
More information about the Python-list
mailing list