This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Simple extension to iter(): iter() returns empty generator
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: brett.cannon, jneb, rhettinger
Priority: normal Keywords:

Created on 2015-09-22 15:08 by jneb, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg251324 - (view) Author: Jurjen N.E. Bos (jneb) * Date: 2015-09-22 15:08
When looking for a "neat" way to create an empty generator, I saw on stackOverflow that the crowd wasn't sure what was the "least ugly" way to do it.
Proposals where:
def emptyIter(): return; yield
or
def emptyIter(): return iter([])

Then it struck me that a trivial extension to the iter() built-in would be to allow to call it without arguments, thus giving a simple to understand empty iterator, and allowing:
def emptyIter(): return iter()
(And, of course, this function would not need to exist in any reasonable program in that case.)

The implementation would be trivial, I assume.
msg251327 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2015-09-22 15:35
I'm not sure that python needs a neater way to make an empty generator.  It isn't a common use case and there isn't anything wrong with iter([]).

There are downsides to expanding an API.  It creates yet another thing to learn and remember.  Even now, few developers know about the two-argument form of iter().
msg251329 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-09-22 16:14
I agree with Raymond. Allowing a non-argument iter() runs the risk of someone messing up and forgetting the arguments and yet having no error that they did so. And considering how easy it is to get an iterator of an empty list or tuple I don't see a benefit.

Thanks for the suggestion, Jurjen, but I'm closing this as rejected.
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69402
2015-09-22 16:14:00brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg251329

resolution: rejected
2015-09-22 15:35:59rhettingersetassignee: rhettinger

messages: + msg251327
nosy: + rhettinger
2015-09-22 15:16:48zach.waresetversions: - Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5
2015-09-22 15:08:36jnebcreate