Operator Precedence/Boolean Logic
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jun 23 03:20:15 EDT 2016
On Thursday 23 June 2016 14:47, Lawrence D’Oliveiro wrote:
> On Thursday, June 23, 2016 at 3:12:52 PM UTC+12, Larry Hudson wrote:
>> On 06/22/2016 12:42 AM, Lawrence D’Oliveiro wrote:
>>> * boolean operators don’t have to operate on boolean values. The
>>> language spec
>>> <https://docs.python.org/3/reference/expressions.html#boolean-operations>
>>> says:
>>>
>>> “...the following values are interpreted as false: False, None, numeric
>>> zero of all types, and empty strings and containers (including strings,
>>> tuples, lists, dictionaries, sets and frozensets). All other values are
>>> interpreted as true.”
>>>
>>> I feel that’s a needlessly complicated rule.
As I described in my earlier email, it isn't complicated, at least not the way
builtins are modelled.
>>> 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.
>
> Tightening it up would rule out a whole class of common errors,
Hardly common. The only two exceptions I've seen are:
- people surprised by midnight being false, but that is fixed in 3.5;
- people surprised that empty or exhausted iterables don't evaluate as false
and the first of those is pretty rare.
But even if you were right, and I disagree that you are, "fixing" this would
break backwards compatibility and cause vast amounts of working code to stop
working. That is much worse than the "problem".
> from
> misunderstanding (or forgetting) the rule about what exactly gets interpreted
> as true and what as false <https://bugs.python.org/issue13936>
> <http://stackoverflow.com/questions/28116931/datetime-time0-0-evaluates-as-
false-in-boolean-context>.
Or... we could fix time objects, as was done.
--
Steve
More information about the Python-list
mailing list