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

Steven D'Aprano steve at pearwood.info
Sun Apr 20 19:04:21 CEST 2014


On Sun, Apr 20, 2014 at 03:07:39PM +0000, Kristján Valur Jónsson wrote:

> Does one ever use iteritems() et al without first invoking iter() on 
> it?  

I can't speak for others, but I never invoke iteritems *with* iter(). 
What would be the point? iteritems is documented as returning an 
interator.

# never this
for key, value in iter(mydict.iteritems()): ...

# but this
for key, value in mydict.iteritems(): ...


> I.e. is it important that it is an iterator, rather than an 
> iterable? I think we could easily relax that requirement in the pep 
> and solve 99% of the use cases.

And the other 1% of cases would be a land-mine waiting to blow the 
user's code up.

Would it actually solve 99% of the use cases? Or only 90%? Or 50%? How 
do you know?

In Python 2.7 iteritems() etc is documented as returning an iterator. 
That's a promise of the language, and people will rely on it. But they 
won't be able to rely on that promise in polygot 2+3 code -- exactly the 
use-case this PEP is trying to satisfy -- because the promise to return 
an iterator will be broken in 3.

It would be actively misleading, since Python 3's iteritems() would 
return a view, not an iter, and it would fail at solving the backwards 
compatibility issue since views and iterators are not interchangeable 
except for the most basic use of iteration.



More information about the Python-Dev mailing list