Generator definition syntax (was: Syntax for one-line "nonymous" functions)

Alexey Muranov alexey.muranov at gmail.com
Tue Apr 2 03:21:42 EDT 2019


On mar., Apr 2, 2019 at 4:31 AM, python-list-request at python.org wrote:
> Re: ">> Neither i like how a function magically turns into a 
> generator if the
>>>  keyword `yield` appears somewhere within its definition.
> 
>>  I agree, there should have been a required syntactic element on the 
>> "def"
>>  line as well to signal it immediately to the reader. It won't stop 
>> me from using them, though."
> 
> One way to save people looking at the code from having to look 
> through a function for a yield statement to see if it is a generator 
> would be to add a """doc string""" immediately after the function 
> def, saying that it is a generator
> and describing what it does.  I realize I'm calling on the programmer 
> to address this issue by adding doc strings.  Nonetheless adding doc 
> strings is a good habit to get in to.
> --- Joseph S.

And even if Python did not have docstrings, the programmer could still 
use comments to tell a fellow programmer what kind of code the fellow 
programmer is looking at. Even languages like Brainfuck have comments 
:).

Alexey.





More information about the Python-list mailing list