[Python-Dev] PEP 469: Restoring the iterkeys/values/items() methods

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Apr 20 01:46:08 CEST 2014


Stephen J. Turnbull wrote:
> Benjamin Peterson writes:
> 
>  > > I suppose there's no way to get the compiler to both make "for x in d"
>  > > work as above, and make "for k, v in d" be equivalent to Python 2's
>  > > "for k, v in d.iteritems()"?
> 
> it would change the meaning of currently correct
> programs, so it's a non-starter.

Maybe what's wanted is a function analogous to enumerate() for
mappings instead of sequences. Picking a semi-arbitrary name
for now:

    for k, v in tabulate(d):
       ...

It could be special-cased to recognise dicts and do the appropriate
thing for the Python version concerned. If it doesn't recognise the
type, it would fall back to a generic implementation like

    for k in d:
       v = d[k]:
          ...

-- 
Greg


More information about the Python-Dev mailing list