ternary operator

David LeBlanc whisper at oz.net
Thu Feb 6 23:15:46 EST 2003


<snip>

> What exactly do you mean by "short circuiting conditionals"?
>
> I thought it just meant something like C's ternary operator.
> Algol 60 had something like that.  You could say
>
>    x = if a>=0 then sqrt(a) else sqrt(-a);
>
> which would take the square root of the absolute value of a.
> Short circuiting just means the non-selected branch doesn't get evaluated.

Incorrect. Short circuiting refers to a complex conditional aborting
evaluation as soon as it fails (technically, as soon as the result is
determinable), as in:

	if a > b and c < d:
		print e

If a <= b then c will never be evaluated against d: that's the short
circuit. Life gets "interesting" if there is a side effect in the
unevaluated portion of the conditional:

	if a > b and c < d():
		print e

d() might never get called. Not good if d() does something you depend on.

Dave LeBlanc
Seattle, WA USA






More information about the Python-list mailing list