[Python-ideas] Re: Opt-in “iter def” and/or “gen def” syntax for generator functions
I don't really disagree with most of what you wrote! And agree that decorators, specifically, are a pretty good solution within the scope of an individual package. But I would quibble with this: >How fundamental is it that THIS function is a generator, rather than simply >that > it returns an iterator (or that it returns a generator/coroutine object, etc)? The delayed execution of a generator function is, IMO very, very different than a regular function! ``` def f(): result = side_effect1() yield 0 yield 1 return result ``` vs. ``` def f(): side_effect() return range(2) ``` These are contrived examples, obviously, but I've specifically encountered issues like this when working with code that handles streaming data - from a db or REST API, for instance. My experience of this type of code is often along the following lines: ``` def stream_rows(self): [mutex handling ...] [retry logic] [more mutexes?] [some timeout stuff] [...] ``` I don't mean to belabor the point - the decision in PEP 255 is pretty definitive, and this was more or less a "modest proposal" anyway. I do appreciate the responses and discussion! Thanks, Aaron ___ 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/JMAWFCPYBW5BN2C33KNBE563YHRZZPXG/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Opt-in “iter def” and/or “gen def” syntax for generator functions
Thanks for your reply. > What's the advantage? I brought this up thinking about explicitness and readability. Say you want to figure out what this function is doing: def foo() -> t.Iterator[T]: [... 300 lines of code] ``` Is this a generator function? I'd argue that whether it's a generator function or not is fundamental to being able to read it. The type hint alone doesn't tell you whether you're looking at a generator function or not - it might just construct and return an iterator. So, you have to look for a `yield`. And if "yield" is somewhere in the function body, it will abruptly change the semantics of the entire definition. This feels a like spooky-action-at-a-distance to me - I'd much rather have the information up top in the function declaration, the same way that `async def` declares a coroutine function. However: this was actually discussed in PEP 255, where there was a decision *not* to introduce a new keyword for generator functions. From the BDFL decision: > No argument on either side is totally convincing, so I have consulted > my language designer’s intuition. It tells me that the syntax proposed > in the PEP is exactly right - not too hot, not too cold. But, like the Oracle > at Delphi in Greek mythology, it doesn’t tell me why, so I don’t have a > rebuttal for the arguments against the PEP syntax. - Aaron ___ 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/MVNK3UJHNFRPQZLHBMV47MLLT5P6OM25/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Opt-in “iter def” and/or “gen def” syntax for generator functions
After getting used to writing async functions, I’ve been wanting use a similar syntax to declare generator functions. Something along the lines of `iter def my_iterator() -> T` and/or `gen def my_generator() -> (T, U, V)` Obviously, for backwards compatibility, this would need to be optional or have an opt-in mechanism. Would a feature like this be at all within the realm of possibility? I’d be happy to write up a much longer discussion if so. (I found a short discussion of such a feature in the archives about 8 years ago[1]. But, since it predates both `async def` and the current type checker regime, I thought it might be worth discussing. Apologies if I missed any more recent discussions.) Thanks, Aaron [1] https://mail.python.org/archives/list/python-ideas@python.org/thread/OVIHVRKFUN4KMDTVSIAAN2CGR7VXFGQS/#GE6RDNWTR4PPKSMKSGMCFBFUJ42FWWV6 ___ 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/YGFRUHDAOXPHUKODV5VOPQZGJFLRBWLT/ Code of Conduct: http://python.org/psf/codeofconduct/