(test) ? a:b

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Oct 25 20:09:50 EDT 2014


On 26/10/2014 01:01, Chris Angelico wrote:
> On Sun, Oct 26, 2014 at 10:53 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
>> 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.
>
> Would it be more readable thus?
>
> if len([opt for opt in (option_a, option_b, option_c) if opt]) != 1:
>
> Because that's what it's doing. I think treating bools as numbers is
> fine. Matter of opinion I guess.
>
> ChrisA
>

Horrible IMHO, it just doesn't fit in my mind set.  Still each to their own.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list