PEP308 - preference for 'x if c else y' over 'c then x else y'

Stephen Horne intentionally at blank.co.uk
Sun Feb 16 18:50:18 EST 2003


On Sun, 16 Feb 2003 15:38:34 GMT, Andrew Koenig <ark at research.att.com>
wrote:

>Side effects are not the only reason that order of evaluation matters.
>
>Even in a purely functional world, consider the following expression
>
>        f(x) if e(x) else g(x)
>
>Here, f and g are partial functions over x, and all possible values of
>x are in union(dom(f), dom(g)).  However, not all possible values of x
>are in intersection(dom(f), dom(g)).  In particular, when e(x) is true,
>x is in dom(f) but not necessarily in dom(g), and when e(x) is false,
>x is in dom(g) but not necessarily in dom(f).
>
>Now, in order to evaluate this expression, it is necessary to evaluate
>f(x) only if e(x) is true, and to evaluate g(x) only if e(x) is false.
>
>Side effects don't enter the picture.

Yes - as I said in my post, and as you even included in the quote...

>Stephen> (except
>Stephen> in that lazy evaluation can ignore subexpressions which are
>Stephen> not needed, but which might otherwise trigger errors).

In your example, if e(x) is true, f(x) is not needed as it cannot
affect the result - but its evaluation could trigger an error.

So what are you contradicting me on, exactly?

-- 
steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list