writable iterators?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 22 21:53:17 EDT 2011


On Thu, 23 Jun 2011 09:30 am Thomas 'PointedEars' Lahn wrote:

> Mel wrote:
> 
>> Steven D'Aprano wrote:
>>> 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.
> 
> <http://docs.python.org/reference/simple_stmts.html#the-yield-statement>
> 
> You could have tried to debug.

I think you have missed the point of Mel's comment. He knows what the yield
statement does. He doesn't know what it would mean to "write to" an
iterator like things().

Neither do I.



-- 
Steven




More information about the Python-list mailing list