[Python-Dev] PEP 479: Change StopIteration handling inside generators (original) (raw)

Ethan Furman ethan at stoneleaf.us
Sat Nov 22 21:04:20 CET 2014


On 11/22/2014 06:31 AM, Nick Coghlan wrote:

A particularly relevant variant of the idiom is the approach of writing "iter" directly as a generator, rather than creating a separate custom iterator class. In that context, the similarities between the iter implementation and the corresponding explicit next implementation is a beneficial feature.

https://docs.python.org/3/reference/datamodel.html?highlight=iter#object._iter_

This method is called when an iterator is required for a container. This method should return a new iterator object that can iterate over all the objects in the container. For mappings, it should iterate over the keys of the container, and should also be made available as the method keys().

Iterator objects also need to implement this method; they are required to return themselves. For more information on iterator objects, see Iterator Types.

Unless the object is itself at iterator, the iter method is allowed to return any iterator object; whether that iterator is constructed by a separate class entirely, or by using the iter() function, or by writing a generator, should have no bearing on how we write generators themselves.

-- Ethan

-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: OpenPGP digital signature URL: <http://mail.python.org/pipermail/python-dev/attachments/20141122/bbe8bbdc/attachment.sig>



More information about the Python-Dev mailing list