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

Steven D'Aprano steve at pearwood.info
Mon Apr 21 03:38:57 CEST 2014


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

> Well, "for i in x" and other iteration constructs already call "iter 
> ()" on their iterable. That's the point. Unless you want to manually 
> iterate using "next ()" then the distinction between an iterable and 
> an iterator is academic.

Do you think that for loops are the only way that iteritems and 
friends get used? You can't know that. I know from my own code that it 
isn't true. Iterators are first class objects like any other object, and 
they get put into tuples, passed around as arguments to functions, 
stored in variables, and so on. They get tested to be iterators, e.g.:

    assert the_items is iter(the_items)

and, yes, sometimes people call next() on them directly.

There is a reason that viewitems() etc. were added to Python 2.7 rather 
than just changing iteritems() to return a view. It would break backward 
compatibility, and break people's code. How much code? I don't know, but 
*any* breakage defeats the purpose of this suggestion. The whole point 
of this PEP is for the iter* methods to work the same way in Python 2 
and Python 3, not "nearly the same, sorry if it breaks your code".

If we wanted "nearly the same", we already have that: dict.items() in 
Python 2 and 3 is already nearly the same, they are both iterable. 
Nearly the same is not good enough.

If this is a cunning plan to make Nick's suggestion sound better by 
suggesting an even worse alternative, it's working :-)



-- 
Steven


More information about the Python-Dev mailing list