
On 6/1/2022 9:59 AM, Chris Angelico wrote:
31.05.22 16:21, Chris Angelico пише:
On Tue, 31 May 2022 at 23:00, Aaron L via Python-ideas <python-ideas@python.org> wrote:
After getting used to writing async functions, I’ve been wanting use a similar syntax to declare generator functions. What's the advantage? You can just use normal function syntax to define them, and it works correctly. Do you need the ability to declare that it's a generator even without any yields in it? The advantage is that you cannot accidentally turn a function into a generator by adding "yield". If the result of the call is ignored (it is expected to be None), this bug can live a long time. It is a common issue: test containing yield always passes. Since 3.11 the unittest module emits a warning if a test method returns not None, but it will not solve all problems: the test can call helpers, and if they are generators, the call is virtually no-op. This error can also occur in non-test code. That might be nice, but without a massive backward compatibility break (or another keyword for non-generator functions), it can't happen. Is
On Wed, 1 Jun 2022 at 23:55, Serhiy Storchaka <storchaka@gmail.com> wrote: there any advantage to being able to declare that it must be a generator (as opposed to simply returning a generator object)?
Serhiy explains the issues above, and I've been bitten by it. def fn(): # Do something with side effects, or maybe mutating parameters. print("foo") yield 3 fn() # Called for the side effects, but not iterated over. It does not print "foo". I agree that the compatibility issues are large and tricky. Eric
Maybe I just don't work on the right sorts of codebases.
ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/XBY4IP... Code of Conduct: http://python.org/psf/codeofconduct/