what is it, that I don't understand about python and lazy evaluation?

David House dmhouse at gmail.com
Thu Aug 13 13:11:18 CEST 2009

2009/8/13 Erik Bernoth <erik.bernoth at googlemail.com>:
> after 14 it is not nessesary to evaluate evens() any further.

How does Python know this? I.e. how does it know that evens() will
always yield things in ascending order? For example, I could write an
iterator like this:

def my_iter():
    for i in [0,2,4,6,8,10,12,14,16,18,1,3,5]:
        yield i

Now, imagine I do [i for i in my_iter() if i < 15]. If you quit
iterating after `i' becomes 16, you'll miss the valid numbers 1, 3, 5
at the end of the list!


More information about the Python-list mailing list