writable iterators?

Ethan Furman ethan at stoneleaf.us
Wed Jun 22 16:04:40 EDT 2011


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?

No.  e = blah will rebind the indentifier 'e' with 'blah' whatever that 
is.  That is how python works.

Now, if e is mutable, say a list, you can do

    e.append(blah)

and, since the name 'e' is not being rebound, you would see the change 
in 'sequence'.

~Ethan~



More information about the Python-list mailing list