what is it, that I don't understand about python and lazy evaluation?
erik.bernoth at googlemail.com
Thu Aug 13 11:32:53 CEST 2009
look at the following code:
# iterator returning even numbers
i = 0
i += 2
# now get all the even numbers up to 15
L = [n for n in evens() if n < 15]
Isn't it strange, that this code runs (in a lazy language) for eternity? I
would expect python to to spit out (in no time):
[0, 2, 4, 6, 8, 10, 12, 14]
after 14 it is not nessesary to evaluate evens() any further.
I really started to ask myself if python really is lazy, but everything else
I wrote in lazy style still worked. Example:
>> def test(txt, retval):
.. return retval
>>> test(1, True) or test(2, True)
>>> test(1, True) and test(2, True)
Can anybody explain what happens with evens()?
PS: The code comes from a list post from 2006. You find it here:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list