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

Kristján Valur Jónsson kristjan at ccpgames.com
Sun Apr 20 21:27:08 CEST 2014


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.


Sent from the æther.

-------- Original message --------
From: Steven D'Aprano
Date:20/04/2014 17:05 (GMT+00:00)
To: python-dev at python.org
Subject: Re: [Python-Dev] PEP 469: Restoring the iterkeys/values/items() methods

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.

_______________________________________________
Python-Dev mailing list
Python-Dev at python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/kristjan%40ccpgames.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140420/fefa8beb/attachment.html>


More information about the Python-Dev mailing list