[Python-Dev] PEP 3000 and iterators

Guido van Rossum guido at python.org
Mon Sep 12 16:37:30 CEST 2005


On 9/11/05, Delaney, Timothy (Tim) <tdelaney at avaya.com> wrote:
> James Y Knight wrote:
> 
> > Just to be clear, I do not want nor expect this. I wish to be able to
> > specifically modify code with full knowledge of what has changed in
> > Py3.0 such that it will work with both Py2.X and Py3.0.
> 
> If you want these things to work in 2.x and 3.0, just use
> iter(dict_instance) and list(dict_instance) as appropriate.

Simpler still, just use di.keys() but make sure you're only using the
result to iterate over once without modifying the dict's key set. Or
if you *have* to avoid creating a list in Py2.x, write your code to
iterate over the dict itself even if you'd like itervalues or
iteritems; you can always get the value explicitly by indexing the
dict.

IOW use the API whose name will remain but don't rely on the
functionality that will change.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list