Lazy means worked out when asked
Not met yet
Lazy means nothing is worked out until something asks for it. A generator only ever holds the value it's on, so it can stream a file of any length, or count for ever, as long as you only ask for what you need. range and generator expressions like (g * 2 for g in amounts) are lazy too. A list comprehension is not: it builds every item first.
Try it
main.pyOutput
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks a generator with a while True loop freezes the program as soon as it is made.
- Thinks a generator expression in round brackets builds a list or tuple of every value straight away.
- Thinks a used-up iterator or generator starts again from the beginning when it is looped over a second time.