Why "yield from" does not work as expected in all() or any()?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 Question
01:57 Accepted answer (Score 2)
02:51 Thank you
--
Full question
https://stackoverflow.com/questions/3373...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping
--
Chapters
00:00 Question
01:57 Accepted answer (Score 2)
02:51 Thank you
--
Full question
https://stackoverflow.com/questions/3373...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x
#avk47
ACCEPTED ANSWER
Score 2
func1 is a function returning a generator while func2 and func3 are regular functions:
>>> type(func1())
<class 'generator'>
>>> type(func2())
All 3 ok !
<class 'NoneType'>
That's because:
ok3 = all((yield from yield_random()) for i in range(3))
is actually equivalent to:
def _gen():
for i in range(3):
r = yield from yield_random()
yield r
ok3 = all(_gen())
This code doesn't yield anything since the yield from statement is encapsulated in a generator. You can even run it outside of a function, in your console for instance:
>>> all((yield from yield_random()) for i in range(3))
False
Though it probably doesn't do what you expect:
>>> list((yield from yield_random()) for i in range(3))
['OK', True, False, False]