lazy evaluation in Python

Tim Peters tim_one at email.msn.com
Sat Sep 9 14:08:10 EDT 2000


[Fernando Rodriguez]
>     What's the best way to implement lazy evaluation (streams or
> lazy lists)in Python? O:-)

What's "best"?  Most like Scheme, most like Haskell, fastest, cleanest, most
Pythonic, ...?  See

    http://www.deja.com/getdoc.xp?AN=590446915&fmt=text

for a thread containing a simple implementation of a Python Stream class,
along with "the usual" examples (infinite stream of 1s, stream of ints,
stream of Fibonacci numbers, stream of primes, increasing stream of ints of
the form 3**i * 5**j * 7**k w/ i,j,k>=0, lazy map and filter and zip, etc).
This is a Scheme-like implementation wrapped in a Python class.  Streams are
easy to manipulate for the end-user this way, but clumsy for the Stream
author to write.  That's another axis along which "best" could be measured.
Laziness is simply unnatural in Python, and as I said in the article, if you
*really* want to do lazy, use Haskell.  I say the same thing to Scheme
programmers, but for some unfathomable reason they get offended <wink>.

languages-like-people-are-born-lazy-not-made-ly y'rs  - tim






More information about the Python-list mailing list