diff --git a/native-datatypes.html b/native-datatypes.html index 5893d58..a93a185 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -37,7 +37,7 @@ body{counter-reset:h1 2}
Booleans are either true or false. Python has two constants, True and False, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if statements), Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
+
Booleans are either true or false. Python has two constants, cleverly True and False, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like if statements), Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
For example, take this snippet from humansize.py:
if size < 0:
raise ValueError('number must be non-negative')
@@ -52,6 +52,20 @@ body{counter-reset:h1 2}
>>> size = -1
>>> size < 0
True
+Due to some legacy issues left over from Python 2, booleans can be treated as numbers. True is 1; False is 0.
+
+>>> True + True +2 +>>> True + False +1 +>>> True * False +0 +>>> True * False +Traceback (most recent call last): + File "<stdin>", line 1, in <module> +ZeroDivisionError: int division or modulo by zero+
Ew, ew, ew! Don’t do that. Forget I even mentioned it. +
⁂