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

jcm joshwaywithoutspam at myway.com
Tue Feb 18 09:01:38 EST 2003


Andrew Koenig <ark at research.att.com> wrote:
> 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.

What happens if f(x) is evaluated if e(x) is false?  An exception?
I'd consider that a side effect.




More information about the Python-list mailing list