Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8309

Re: writable iterators?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'languages,': 0.03; 'memory.': 0.05; 'behave': 0.07; 'raises': 0.07; 'variable,': 0.07; 'python': 0.08; 'mutable': 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; 'stl': 0.09; 'syntax.': 0.09; 'underlying': 0.09; 'output': 0.11; 'syntax': 0.11; 'c++': 0.12; 'wed,': 0.13; 'wrote:': 0.15; 'accesses': 0.16; 'assignment.': 0.16; 'capability.': 0.16; 'computation': 0.16; 'instances,': 0.16; 'iterable,': 0.16; 'iteration.': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterators,': 0.16; 'numpy': 0.16; 'promises': 0.16; 'case.': 0.16; 'pm,': 0.16; 'suggest': 0.17; 'advance': 0.18; 'input': 0.21; 'variable': 0.21; 'badly': 0.22; 'loop': 0.22; 'so.': 0.22; "doesn't": 0.22; 'indexing': 0.23; 'sequences.': 0.23; 'works.': 0.23; 'worked': 0.24; 'creating': 0.24; 'index': 0.25; 'changed': 0.25; "i'm": 0.27; 'keeps': 0.28; 'bugs': 0.28; 'depends': 0.28; '(and': 0.28; 'accessible': 0.29; 'explicitly': 0.29; 'lists': 0.29; 'object': 0.30; 'example': 0.30; '(maybe': 0.30; '22,': 0.30; 'differences': 0.30; 'kelly': 0.30; 'protocol.': 0.30; 'second,': 0.30; 'functional': 0.31; 'separate': 0.31; 'subject:?': 0.31; 'values': 0.31; 'google': 0.32; 'it.': 0.33; 'does': 0.33; 'source': 0.33; 'actually': 0.33; 'header:User-Agent:1': 0.34; 'header:X -Complaints-To:1': 0.34; 'there': 0.34; 'to:addr:python-list': 0.34; "can't": 0.34; 'couple': 0.34; 'things': 0.35; 'assignment': 0.35; 'explicit': 0.35; 'languages.': 0.35; 'uses': 0.35; 'question': 0.35; 'actual': 0.35; 'supposed': 0.35; 'skip:" 10': 0.36; 'none': 0.36; 'similar': 0.37; 'doing': 0.37; 'but': 0.37; 'using': 0.38; 'another': 0.38; 'could': 0.38; 'received:org': 0.38; 'steven': 0.38; 'think': 0.38; 'subject:: ': 0.38; 'case,': 0.38; 'unless': 0.39; 'header:Mime-Version:1': 0.39; 'data': 0.39; 'ways': 0.39; 'allows': 0.39; 'basic': 0.39; 'to:addr:python.org': 0.39; "there's": 0.39; 'case': 0.40; 'where': 0.40; 'would': 0.40; 'more': 0.60; 'forget': 0.61; 'target': 0.61; 'results': 0.62; 'manner': 0.64; 'believe': 0.66; 'demand': 0.66; 'jun': 0.67; 'works,': 0.68; 'imagine': 0.71; 'serial': 0.71; 'collection': 0.72; 'concept': 0.73; 'anywhere,': 0.73; 'teach': 0.76; 'calculations': 0.84; 'collection,': 0.84; 'dense': 0.84; 'dict,': 0.84; 'idiom': 0.84; 'problematic': 0.84; 'received:139': 0.84; 'regard.': 0.84; 'streams': 0.84; 'structures.': 0.84; 'advancement': 0.91; 'step.': 0.91; 'writing,': 0.91; 'step,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Neal Becker <ndbecker2@gmail.com>
Subject Re: writable iterators?
Followup-To gmane.comp.python.general
Date Thu, 23 Jun 2011 12:06:10 -0400
References <mailman.296.1308770918.1164.python-list@python.org> <4e026497$0$29975$c3e8da3$5496439d@news.astraweb.com> <BANLkTim9DauArE8dyFL22stotoTGfipBmg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host 139.85.237.117
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.326.1308845195.1164.python-list@python.org> (permalink)
Lines 93
NNTP-Posting-Host 82.94.164.166
X-Trace 1308845195 news.xs4all.nl 14144 [::ffff:82.94.164.166]:34498
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8309

Followups directed to: gmane.comp.python.general

Show key headers only | View raw


Ian Kelly wrote:

> On Wed, Jun 22, 2011 at 3:54 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> 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.
> 
> The example syntax is a non-starter, but there's nothing wrong with
> the basic idea.  The STL of C++ uses output iterators and a quick
> Google search doesn't turn up any "harmful"-style rants about those.
> 
> Of course, there are a couple of major differences between C++
> iterators and Python iterators.  FIrst, C++ iterators have an explicit
> dereference step, which keeps the iterator variable separate from the
> value that it accesses and also provides a possible target for
> assignment.  You could say that next(iterator) is the corresponding
> dereference step in Python, but it is not accessible in a for loop and
> it does not provide an assignment target in any case.
> 
> Second, C++ iterators separate out the dereference step from the
> iterator advancement step.  In Python, both next(iterator) and
> generator.send() are expected to advance the iterator, which would be
> problematic for creating an iterator that does both input and output.
> 
> I don't think that output iterators would be a "disaster" in Python,
> but I also don't see a clean way to add them to the existing iterator
> protocol.
> 
>> If you want to change the source iterable, you have to explicitly do so.
>> Whether you can or not depends on the source:
>>
>> * 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);
> 
> No, an iterator is an object that allows traversal over a collection
> in a manner independent of the implementation of that collection.  In
> many instances, especially in Python and similar languages, the
> "collection" is abstracted to an operation over another collection, or
> even to the results of a serial computation where there is no actual
> "collection" in memory.
> 
> Iterators are not lazy sequences, because they do not behave like
> sequences.  You can't index them, you can't reiterate them, you can't
> get their length (and before you point out that there are ways of
> doing each of these things -- yes, but none of those ways use
> sequence-like syntax).  For true lazy sequences, consider the concept
> of streams and promises in the functional languages.
> 
> In any case, the desired behavior of an output iterator on a source
> iterator is clear enough to me.  If the source iterator is also an
> output iterator, then it propagates the write to it.  If the source
> iterator is not an output iterator, then it raises a TypeError.
> 
>> * 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
> 
> Unless the underlying collection is a dict, in which case I need to do:
> 
> for k, v in d.items():
>     d[k] = v + 42
> 
> Or a file:
> 
> for line in f:
>     # I'm not even sure whether this actually works.
>     f.seek(-len(line))
>     f.write(line.upper())
> 
> As I said above, iterators are supposed to provide
> implementation-independent traversal over a collection.  For writing,
> enumerate fails in this regard.


While python may not have output iterators, interestingly numpy has just added 
this capability.  It is part of nditer.  So, this may suggest a syntax.

There have been a number of responses to my question that suggest using indexing 
(maybe with enumerate).  Once again, this is not suitable for many data 
structures.  c++ and stl teach that iteration is often far more efficient than 
indexing.  Think of a linked-list.  Even for a dense multi-dim array, index 
calculations are much slower than iteration.

I believe the lack of output iterators is a defienciency in the python iterator 
concept.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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