+ 1

generator function in python

>>> g = (2**x for x in range(100)) >>> next(g) 1 >>> next(g) 2 >>> next(g) 4 >>> next(g) 8 >>> next(g) 16 I had came across this example of generator function.How this can be a generator function,it doesn't have yield keyword. How is this functioning ?

8th May 2019, 8:05 PM
harshit
harshit - avatar
1 Respuesta
+ 11
It's a generator expression. Basically a list comprehension with () instead of []. It doesn't need a yield statement just like a lambda doesn't need a "return"
8th May 2019, 8:08 PM
Anna
Anna - avatar