[Python-Dev] PEP 276 (simple iterator for ints)

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Mon Jul 5 01:31:10 CEST 2004


Andrew Koenig wrote:

> for i in myList.keys():
> 
> would be even better, because it allow the same usage for dict and
> list.  Of course that wouldn't generalize to other sequences that
> support len. 

Hmm - time to propose a new builtin (can't remember this being proposed
before, though I know we've discussed adding keys() to lists).

keys(mapping)

Returns an iterator of the keys of the mapping. For any sequence that
implements __len__, returns an iterator of the integers 0..__len__. For
any object that implements keys() or iterkeys(), returns the appropriate
iterator.

I would also suggest that __keys__ might be a useful magic method.

There might be a use for values(mapping) as well, but I don't think it's
as useful.

Tim Delaney



More information about the Python-Dev mailing list