Equivalent code to the bool() built-in function

Chris Angelico rosuav at gmail.com
Tue Apr 19 02:26:50 EDT 2011


On Tue, Apr 19, 2011 at 4:23 PM, Kushal Kumaran
<kushal.kumaran+python at gmail.com> wrote:
>> if a + b + c + d != 1:
>>    raise ValueError("Exactly one of a, b, c or d must be true.")
>>
>
> Unless you're sure all of a, b, c, and d are boolean values, an int
> with a negative value slipping in could result in the sum equaling 1,
> but more than one of the variables evaluating to True in boolean
> contexts.

If they're all expressions, then you can easily guarantee that.

ChrisA



More information about the Python-list mailing list