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

Andrew Koenig ark at research.att.com
Sun Feb 16 10:38:34 EST 2003


Stephen> I don't see things that way. I have more of a functional view
Stephen> of expressions. In general, I think I shouldn't need to worry
Stephen> about the order of evaluation - only about the meaning and
Stephen> the readability. In expressions, evaluation order only makes
Stephen> a difference to the meaning if there are side effects (except
Stephen> in that lazy evaluation can ignore subexpressions which are
Stephen> not needed, but which might otherwise trigger errors). Having
Stephen> functions with side effects is often considered bad style,
Stephen> partly for this exact reason.

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.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list