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


Groups > comp.lang.python > #8333

Re: writable iterators?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!not-for-mail
From Chris Torek <nospam@torek.net>
Newsgroups comp.lang.python
Subject Re: writable iterators?
Date 23 Jun 2011 22:17:26 GMT
Organization None of the Above
Lines 53
Message-ID <iu0e1m02lsa@news2.newsguy.com> (permalink)
References <mailman.296.1308770918.1164.python-list@python.org> <iu00fs1dhg@news3.newsguy.com>
NNTP-Posting-Host pdb88319f3959d0ddcc7f96bad045841a4b5c34c77fecda98.newsdawg.com
X-Newsreader trn 4.0-test76 (Apr 2, 2001)
Originator torek@elf.torek.net (Chris Torek)
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8333

Show key headers only | View raw


In article <iu00fs1dhg@news3.newsguy.com> I wrote, in part:
>Another possible syntax:
>
>    for item in container with key:
>
>which translates roughly to "bind both key and item to the value
>for lists, but bind key to the key and value for the value for
>dictionary-ish items".  Then ... the OP would write, e.g.:
>
>    for elem in sequence with index:
>        ...
>        sequence[index] = newvalue
>
>which of course calls the usual container.__setitem__.  In this
>case the "new protocol" is to have iterators define a function
>that returns not just the next value in the sequence, but also
>an appropriate "key" argument to __setitem__.  For lists, this
>is just the index; for dictionaries, it is the key; for other
>containers, it is whatever they use for their keys.

I note I seem to have switched halfway through thinking about
this from "value" to "index" for lists, and not written that. :-)

Here's a sample of a simple generator that does the trick for
list, buffer, and dict:

    def indexed_seq(seq):
        """
        produce a pair
            <key_or_index> <value>
        such that seq[key_or_index] is <value> initially; you can
        write on seq[key_or_index] to set a new value while this
        operates.  Note that we don't allow tuple and string here
        since they are not writeable.
        """
        if isinstance(seq, (list, buffer)):
            for i, v in enumerate(seq):
                yield i, v
        elif isinstance(seq, dict):
            for k in seq:
                yield k, seq[k]
        else:
            raise TypeError("don't know how to index %s" % type(seq))

which shows that there is no need for a new syntax.  (Turning the
above into an iterator, and handling container classes that have
an __iter__ callable that produces an iterator that defines an
appropriate index-and-value-getter, is left as an exercise. :-) )
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html

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