I have the following problem with python\'s \"all\" and generators:
G = (a for a in [0,1])
all(list(G)) # returns False - as I expected
B
No, it doesn't. The following snippet returns False
G = (a for a in [0,1])
all(G) # returns False
Are you perhaps doing the following
G = (a for a in [0,1])
all(list(G)) # returns False
all(G) # returns True!
In that case, you are exhausting the generator G
when you construct the list, so the final call to all(G)
is over an empty generator and hence returns the equivalent of all([])
-> True
.
A generator can't be used more than once.