Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8339
| 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 | <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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; ':-)': 0.06; '"""': 0.07; 'defines': 0.07; 'received:verizon.net': 0.07; '%s"': 0.09; '[],': 0.09; 'bind': 0.09; 'callable': 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; 'syntax.': 0.09; 'tuple': 0.09; 'syntax': 0.11; 'wrote:': 0.15; '"new': 0.16; '(turning': 0.16; '>>is': 0.16; 'halfway': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'iterator.': 0.16; 'keys.': 0.16; 'numpy': 0.16; 'operates.': 0.16; 'pythonic': 0.16; 'received:east.verizon.net': 0.16; 'sequence,': 0.16; 'translates': 0.16; 'argument': 0.16; 'def': 0.16; 'written': 0.17; 'switched': 0.19; 'appropriate': 0.20; 'seems': 0.20; 'pair': 0.23; 'produces': 0.23; 'slice': 0.23; 'index': 0.25; 'skip:[ 10': 0.26; 'string': 0.26; 'function': 0.26; 'raise': 0.28; 'yield': 0.29; 'classes': 0.30; 'buffer,': 0.30; 'syntax,': 0.30; 'subject:?': 0.31; 'seem': 0.31; 'usual': 0.32; 'chris': 0.32; 'shows': 0.32; "skip:' 10": 0.32; 'handling': 0.33; 'does': 0.33; 'header:User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.34; 'there': 0.34; 'to:addr:python- list': 0.34; '...': 0.34; 'define': 0.35; 'lists,': 0.35; 'sequence': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'would': 0.40; 'here': 0.66; 'roughly': 0.67; 'container': 0.73; 'article': 0.76; '"index"': 0.84; 'exercise.': 0.84 |
| 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 21:10:25 -0400 |
| References | <mailman.296.1308770918.1164.python-list@python.org> <iu00fs1dhg@news3.newsguy.com> <iu0e1m02lsa@news2.newsguy.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 <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.346.1308877841.1164.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1308877841 news.xs4all.nl 14140 [::ffff:82.94.164.166]:35558 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8339 |
Followups directed to: gmane.comp.python.general
Show key headers only | View raw
Chris Torek wrote:
> 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. :-) )
Here is what numpy nditer does:
for item in np.nditer(u, [], ['readwrite'], order='C'):
... item[...] = 10
Notice that the slice syntax is used to 'dereference' the iterator. This seems
like reasonably pythonic syntax, to my eye.
Back to comp.lang.python | Previous | Next — Previous 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