Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'wed,': 0.03; 'received:verizon.net': 0.07; 'python': 0.08; 'mutable': 0.09; 'readable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrote:': 0.14; 'blah': 0.16; 'immutable': 0.16; 'iteration.': 0.16; 'iterator': 0.16; 'iterators,': 0.16; 'received:east.verizon.net': 0.16; 'sequence.': 0.16; 'tuples,': 0.16; 'meant': 0.18; 'loop': 0.22; 'say,': 0.22; 'code.': 0.22; 'indexing': 0.23; 'loop,': 0.23; 'values': 0.25; 'certainly': 0.25; 'changed': 0.25; 'not.': 0.26; 'tried': 0.27; 'example': 0.27; 'subject:?': 0.29; 'elements': 0.29; 'lists': 0.29; 'changed.': 0.30; 'steven': 0.32; 'header:X -Complaints-To:1': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'it?': 0.33; 'regardless': 0.34; 'header:User-Agent:1': 0.35; "d'aprano": 0.35; 'store': 0.35; 'something': 0.37; 'change': 0.37; 'limitation': 0.37; 'sequence': 0.37; 'received:org': 0.38; 'problem.': 0.38; 'but': 0.38; 'cases,': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'allows': 0.40; 'forget': 0.61; 'believe': 0.66; 'demand': 0.66; '"do': 0.67; 'concept': 0.73; 'anywhere,': 0.73; 'sequence:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neal Becker Subject: Re: writable iterators? Followup-To: gmane.comp.python.general Date: Wed, 22 Jun 2011 19:10:39 -0400 References: <4e026497$0$29975$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: pool-70-109-84-16.hag.east.verizon.net User-Agent: KNode/4.4.11 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308784254 news.xs4all.nl 14144 [::ffff:82.94.164.166]:59814 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8249 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.