[Python-3000] Iterators for dict keys, values, and items == annoying :)

Nick Coghlan ncoghlan at gmail.com
Fri Mar 24 12:23:10 CET 2006


Guido van Rossum wrote:
> On 3/23/06, Ian Bicking <ianb at colorstudy.com> wrote:
>> Or a view would work just as well, I suppose.  Maybe you've just made
>> the argument that it should return an iterable, not an iterator.
> 
> Technically an iterator is an iterable, so requiring it to return an
> iterable doesn't solve your problem. Requiring a sequence or a
> collection which may be a view instead of a copy *does* solve it, so I
> propose to go for that. This would also solve the redundancy of having
> iter(d) and iter(d.keys()) return the same thing -- d.keys() would
> return a set (not multiset!) view which has other uses than either d
> or iter(d).

Well, d.keys and d.items would be set views, while d.values would be a 
multiset view.

For sequence views (the equivalent of Java's List.subList), maybe it would 
make sense to have a separate 'seqview' type that gives a view on an arbitrary 
existing sequence. First argument would be the sequence itself, while the 
second argument would be an optional slice that limited the visible sequence 
elements.

Something like:

    >>> data = range(10)
    >>> view_all = seqview(data)
    >>> view_end = view_all[5:10]
    >>> view_end[0] = 10
    >>> data[5]
    10

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list