A possible lazy evaluation system?

Terry Reedy tjreedy at udel.edu
Tue Mar 11 15:38:09 CET 2003

"Stephen Horne" <intentionally at blank.co.uk> wrote in message
news:lmmr6vgoc01vkbvkp6vmbo8d9dnboljunl at 4ax.com...
> It occured to me that, instead of a fully lazy parameter system, a
> simpler approach may be to lable parameters as lambdas.
> You could then write something like...
>   def IF (c, lambda x, lambda y) :
>     if c :
>       return x ()
>     else :
>       return y ()
> ...and call it with...
>   IF (y != 0, x/y, 1000000)
> The 'lambda' asserts that the parameters expression gets converted
> a parameterless lambda instead of being evaluated immediately.
> The same notation in declarations of __call__ could provide the same
> capability to all callables.
> The question is - is this a sane suggestion?

As Python is currently defined, no.  If there is to be a
pre-interpretation compile phase, this requires that the compiler know
at *compile* time what function object will be bound to name IF at
*runtime*.  This is currently impossible.  It requires that the
function object exist at runtime, which it does not, or that the
linkage info be accessible at compilie time through another access
method, which it currently is not and which could be very difficult to
impossible.   It also assume that namespace name IF will always be
bound to the same function object!  This is currently not necessarilyt
true.  Definition names, when present, are not the same as namespace
binding names.

In addition, the function called can be the result of an arbitrarily
complicated expression, with a value not calculable at compile time.
Consider as just one example:
  print eval("lambda x: " + raw_imput("Enter 'x expression': "))(2.5)

Terry J. Reedy

More information about the Python-list mailing list