[Python-ideas] And now for something completely different

Cliff Wells cliff at develix.com
Thu Sep 18 02:39:00 CEST 2008


On Wed, 2008-09-17 at 16:18 -0700, Bruce Leban wrote:


> def IF(c,t,f=lambda:None):
>   if c:
>     return t()
>   else:
>     return f()
> IF(hasattr(spam, 'eggs'), lambda: spam.eggs, lambda: 'not found')
> 

To be clear, the problem is that this leads to brittle code (I want to
*enforce* lazy evaluation in a certain context):

IF(a<100, b**a, 0)

Oops, forgot lambda and nothing crashes but we now constantly evaluate
unbounded values.

>  What's the context of the evaluation? Can it access variables in the
> place where it's evaluated? Maybe there's a special way to do that
> too.  Let's not go down that path.

Sorry, missed answering this: the context would be unchanged.  It would
be evaluated as if it were evaluated at the time of the function call.
This may be a serious implementation problem, but I'll let the experts
answer that.

Regards,
Cliff




More information about the Python-ideas mailing list