writable iterators?
Mel
mwilson at the-wire.com
Wed Jun 22 17:59:47 EDT 2011
Steven D'Aprano wrote:
> On Wed, 22 Jun 2011 15:28:23 -0400, Neal Becker wrote:
>
>> AFAICT, the python iterator concept only supports readable iterators,
>> not write. Is this true?
>>
>> for example:
>>
>> for e in sequence:
>> do something that reads e
>> e = blah # will do nothing
>>
>> I believe this is not a limitation on the for loop, but a limitation on
>> the python iterator concept. Is this correct?
>
> Have you tried it? "e = blah" certainly does not "do nothing", regardless
> of whether you are in a for loop or not. It binds the name e to the value
> blah.
>
>>>> seq = [1, 2]
>>>> for e in seq:
> ... print(e)
> ... e = 42
> ... print(e)
> ...
> 1
> 42
> 2
> 42
>
>
> I *guess* that what you mean by "writable iterators" is that rebinding e
> should change seq in place, i.e. you would expect that seq should now
> equal [42, 42]. Is that what you mean? It's not clear.
>
> Fortunately, that's not how it works, and far from being a "limitation",
> it would be *disastrous* if iterables worked that way. I can't imagine
> how many bugs would occur from people reassigning to the loop variable,
> forgetting that it had a side-effect of also reassigning to the iterable.
> Fortunately, Python is not that badly designed.
And for an iterator like
def things():
yield 1
yield 11
yield 4
yield 9
I don't know what it could even mean.
Mel.
More information about the Python-list
mailing list