Terry J. Reedy <tjre...@udel.edu> added the comment:

I think that this should be closed as rejected, and that Patrick should remove 
the 'return's that are in front of 'yield from ...'.  Then 'yield from x' will 
be a statement, not an expression, and the ()s will not be needed, as they are 
used to differentiate yield expressions from yield statements. Generator 
functions return generators, not return expressions.  The latter are only used 
for the StopIteration .value attribute, which I believe is always None for 
Patrick's code and in any case is never used.  So his code should run the same 
without these 'return's.

The exception for '= yield ...' is similar to '= a, ...' not needing ()s.  This 
is the only exception for yield expressions because this is normally the only 
place a yield expression should be used by itself instead of with other 
operators or functions, where ()s are needed.

The python-ideas thread archive is at
https://mail.python.org/archives/list/python-id...@python.org/thread/L6XRQ5YWAE535JGZH2MF2TD32C65K5ZI/

Andrew Svetlov objected (-1) because to him ()s make the statement more 
readable.

Michael Smith got to the real problem, which is that one should not be using 
"return (yield from x)" unless one is accessing a possibly non-None value 
attribute of the implicitly raised StopIteration exception.

I oppose adding a second no-() exception for such rare (and somewhat confusing) 
expert uses. 

In a generator function, the return value is a generator based on the entire 
body of the function.  Any return statement value becomes the value attribute 
of the StopIteration raised when the generator is exhausted.  In other words, 
'return x' in this context translates to "raise StopIteration(x)' (which is 
illegal to write directly). I am pretty sure that in your code, the value of 
'yield from func' is always None.  In any case, StopIteration().value is never 
used.

To illustrate:

def g0():
    return (yield from ())  # Immediately raise StopIteration(None).

try: next(g0())
except StopIteration as e:
    print(e.value)

# Prints 'None'.

----------
nosy: +terry.reedy
versions: +Python 3.11

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue47147>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to