Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8249
| From | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| Subject | Re: writable iterators? |
| Followup-To | gmane.comp.python.general |
| Date | 2011-06-22 19:10 -0400 |
| References | <mailman.296.1308770918.1164.python-list@python.org> <4e026497$0$29975$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.305.1308784254.1164.python-list@python.org> (permalink) |
Followups directed to: gmane.comp.python.general
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. > Yes, I understand that e = blah just rebinds e. I did not mean this as an example of working code. I meant to say, does Python have any idiom that allows iteration over a sequence such that the elements can be assigned? ... > * iterators are lazy sequences, and cannot be changed because there's > nothing to change (they don't store their values anywhere, but calculate > them one by one on demand and then immediately forget that value); > > * immutable sequences, like tuples, are immutable and cannot be changed > because that's what immutable means; > > * mutable sequences like lists can be changed. The standard idiom for > that is to use enumerate: > > for i, e in enumerate(seq): > seq[i] = e + 42 > > AFAIK, the above is the only python idiom that allows iteration over a sequence such that you can write to the sequence. And THAT is the problem. In many cases, indexing is much less efficient than iteration.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
writable iterators? Neal Becker <ndbecker2@gmail.com> - 2011-06-22 15:28 -0400
Re: writable iterators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-22 21:54 +0000
Re: writable iterators? Mel <mwilson@the-wire.com> - 2011-06-22 17:59 -0400
Re: writable iterators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-06-23 01:30 +0200
Re: writable iterators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-23 11:53 +1000
Re: writable iterators? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-06-23 12:23 +0200
Re: writable iterators? Neal Becker <ndbecker2@gmail.com> - 2011-06-22 19:10 -0400
Re: writable iterators? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-23 11:50 +1000
Re: writable iterators? MRAB <python@mrabarnett.plus.com> - 2011-06-23 01:34 +0100
Re: writable iterators? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-23 09:02 -0600
Re: writable iterators? Neal Becker <ndbecker2@gmail.com> - 2011-06-23 12:06 -0400
Re: writable iterators? Chris Torek <nospam@torek.net> - 2011-06-23 18:26 +0000
Re: writable iterators? Chris Torek <nospam@torek.net> - 2011-06-23 22:17 +0000
Re: writable iterators? Neal Becker <ndbecker2@gmail.com> - 2011-06-23 21:10 -0400
csiph-web