mcherm at mcherm.com
Tue Sep 9 18:16:09 CEST 2003
> [Michael, shouting]
> > NOT TRUE!
> > NEITHER of your options does short-circuit evaluation.
> > Er:
> >>> def one():
> print 1
> return 1
> >>> def two():
> print 2
> return 2
> >>> (True and [one()] or [two()])
> >>> (False and [one()] or [two()])
> [Not condoning the use of this idiom, just correcting a factual error.]
Well, I was shouting to be sure I was heard, because I was pretty
sure I was right, and was worried about spreading misinformation. Well,
I clearly should have worried a bit harder. You are correct, the idiom
(<condition> and [<true-branch>] or [<false-branch>])
_does_ indeed short circuit correctly. And now it's ME who's spreading
Of course it's still an ugly idiom. But there's a distinction between
ugly-but-works and ugly-and-wrong.
-- Michael Chermside
More information about the Python-list