Help with Lazy Evaluation

andrew cooke andrew at acooke.org
Sat Jun 7 18:31:07 EDT 2003


you can use generators as a way of generating lazy lists:

http://www.python.org/doc/current/ref/yield.html

otherwise you can use closures:

def stream(x):
    return lambda : (x, stream(x+1))

x = stream(10)
for i in range(5):
    (n, x) = x()
    print n
    
10
11
12
13
14

andrew

junk <junk at geekabytes.net> writes:
> Information Please?
> 
> Hi.  I've looked throught the Python-list archives and I couldn't find
> what I'm looking for.
> 
> Has anyone written a module that supports infinite sets and/or lazy
> evaluation?  If so, could you please let me know?
[...]

-- 
http://www.acooke.org






More information about the Python-list mailing list