On 4/5/2021 8:25 AM, Bischoop wrote:
The return suspends the function execution so how is it that in below
example I got output: <generator object doit at 0x7f57fd2912e0>
def doit():
return 0
yield 0
print(doit())
*Any* use of 'yield' in a function makes the function a generator
function. This is a simple rule that any person, and just as important,
any automated algorithm, can understand. If there were a 'dead
(unreachable) code' exception, a reader or compiler would have to
analyze each use of 'yield' and decide whether it is reachable or not.
And we would have to decide whether just 1 or all 'yield's had to be
reachable.
In the following code, in 3.x, it is also clear that 'yield' is unreachable.
>>> def f():
if False:
yield 0
return 1
>>> f()
<generator object f at 0x0000023A7F3B0CF0>
But 'False' could be a more complex and less obvious but still
equivalent expression, such and 'a and .... and not a'*. Is 'log(a) =
0' tautologically False?
*While 'a and not a' == False in logic, in Python it might raise
NameError. But that would still mean that it is never True, making
'yield 0' still unreachable.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list