Scheme style and Python style [was: Re: Typed Python?]

François Pinard pinard at iro.umontreal.ca
Tue Jul 6 09:50:55 EDT 2004


[Jacek Generowicz]
> François Pinard <pinard at iro.umontreal.ca> writes:

> > Also, there once was a notable distance between Scheme and Python
> > about lazy evaluation [...] but more recent versions of Python offer
> > fairly tractable, almost easy approaches for such things.

> [...] what sort of lazy evaluation ?

I'm not sure of the meaning of the question.  "Lazy evaluation" means
any mechanics by which the language (or system, or programm) waits as
much as possible until a result is needed before computing it.

In Scheme, lazy evaluation is part the language with the `(delay ...)'
and `(force ...)' constructs.  In Python, one could already "simulate"
lazy evaluation by various means, but it turned out to be fairly easy
after iterator generators were introduced, and these are now used all
over the place in Python, often yielding interesting speedup.

One may nest an expression to lazy-evaluate within a generator and use
GENERATOR.next() whenever the value is needed.  As Python now produce
closures more easily than it used to, the generator could be me made to
save part of the evaluation context for later use.  Even nicer, one may
lazily produce a list of values by `yield'-ing them within a loop, and
progressively consume them with a `for'-loop over the generator, say.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard



More information about the Python-list mailing list