(test) ? a:b

Ben Finney ben+python at benfinney.id.au
Sun Oct 26 01:53:52 CEST 2014

Dan Stromberg <drsalists at gmail.com> writes:

> EG, if I have 3 mutually exclusive command line options, I'll do
> something like:
> if option_a + option_b + option_c != 1:
>    sys.stderr.write('{}: -a, -b and -c are mutually exclusive\n'.format(sys.argv[0]))

That is an excellent illustration of why exploiting this accidental
property of True and False leads to obfuscated code. The above test
gives me no clue that we're operating on boolean values, nor that we're
testing for exclusive options.

The error message bears no obvious relation to the test, and if I was
tracking down that error message I'd have no hint from the code why the
test is written that way. An explanatory comment would be needed, but
that's a strong sign IMO that instead the test should be re-written to
be much more obvious.

 \           “Never express yourself more clearly than you are able to |
  `\                                               think.” —Niels Bohr |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list