On 12/06/2023 21:11, Barry wrote:
On 12 Jun 2023, at 16:55, BoppreH via Python-ideas <python-ideas@python.org>
wrote:
Then the empty list creates hard-to-track bugs. I'm familiar with the iterator
protocol and why the behavior above happens, but couldn't it be prevented?
I don’t think so. It is not always a bug that an iterator is empty.
For example this pattern:
args = iter(sys.argv)
progname = next(args)
mandatory_arg = next(args)
for arg in args:
print(‘optional arg’, next(arg))
Your proposal will traceback for the for loop if there are no optional args.
Barry
I don't think so. Using the OP's implementation, the is_exhausted flag
has not been set when the for-loop is entered. It is of course set
after the for-loop has been exited, so a subsequent attempt to iterate
over "args" will raise the Exception.
(The OP's post confirming this arrived while I was typing this e-mail. 😁)
Also the OP's request was for generators, not for any iterator.
Nonetheless, I do not support the proposal, for backward compatibility
reasons. I *can* imagine sane code that relies on the existing behaviour.
Best wishes
Rob Cliffe
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/LZCCVYBTC26DDPNBG6JLZEJOGRXQ742I/
Code of Conduct: http://python.org/psf/codeofconduct/