writable iterators?

Neal Becker ndbecker2 at gmail.com
Thu Jun 23 21:10:25 EDT 2011


Chris Torek wrote:

> In article <iu00fs1dhg at 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.




More information about the Python-list mailing list