[Python 3.8 | Spyder]
I was trying to introduce a simple definition that would either return [1,2,3] or yield the numbers sequentially.
The minimum working code is as follows:
def example(condition):
if condition:
yield 1
yield 2
yield 3
else:
return [1,2,3]
If one attempts to use the def, it will always return a generator. Even if the return appears first in the if/else pair:
def example(condition):
if not condition:
return [1,2,3]
else:
yield 1
yield 2
yield 3
It seems python is not ignoring the presence of the yields even if condition = False. This is an unexpected behaviour.
Aucun commentaire:
Enregistrer un commentaire