Operator Precedence/Boolean Logic

Antoon Pardon antoon.pardon at rece.vub.ac.be
Thu Jun 23 03:58:56 EDT 2016


Op 23-06-16 om 05:59 schreef Steven D'Aprano:
> On Thu, 23 Jun 2016 01:12 pm, Larry Hudson wrote:
>
>> On 06/22/2016 12:42 AM, Lawrence D’Oliveiro wrote:
>> [snip]
>>> I feel that’s a needlessly complicated rule. It would have been simpler
>>> if boolean operators (and conditional expressions like in if-statements
>>> and while-statements) only allowed values of boolean types. But that’s
>>> one of the few warts in the design of Python...
>>>
>> Wart??  I *strongly* disagree.  I find it one of the strengths of Python,
>> it enhances Python's
>> expressiveness.  Of course, everyone is entitled to their own
>> opinion...and this is mine.
> Allowing any value as a truth value is just applying the principle of
> duck-typing to booleans.

What does that mean? As far as I understood, duck typing was that you
could define any class with the same attributes and methods as an other,
often a built in, at which point you could substitute instance of this
new class anywhere you originally expected instance of the old class.

My experience is that this doesn't work with booleans. When I need
real booleans, encountering whatever else that can act like a boolean,
is more often than not an indication something went wrong but the
detection of it going wrong is delayed, because almost everything
can act like a boolean. It is why I have sometime found the need
to write:

    if flag is True:

Because flag needed to be True, not truthy.

-- 
Antoon Pardon




More information about the Python-list mailing list