HomeSearch

Python bool Use (Returns True or False)

This Python example page uses the bool built-in and implements a bool method on a class.
Bool. True means "yes" and false means "no." In Python we rarely need to use the "bool" built-in. Often expressions can just be used directly as bool expressions.
In a class, we can provide a "bool" method that returns True or False. This method indicates how an instance of the class evaluates when used with "bool."
Here, we use the bool built-in with some expressions and constants. We see how Python evaluates the truth of things. A nonempty string is True, but None is False.

Note: With the expressions in the second part of the example, we could omit the "bool" keyword and we would still have the same results.

Python program that uses bool # Compute bools based on expressions and constants. value1 = bool(2 == 3) print(value1) value2 = bool(None) print(value2) value3 = bool("cat") print(value3) print() # Compute bools based on these variables. a = 10 b = 10 value4 = bool(a == b) print(value4) value5 = bool(a != b) print(value5) value6 = bool(a > b) print(value6) Output False False True True False False
Class, def bool. Let us continue with a class. We implement the "__bool__" method. This method must return True or False (this is the bool value a class instance evaluates to).

Here: An instance of the Box class evaluates to True only if the "value" field is equal to 1. All other values will result in False.

Python program that uses class, bool def class Box: def __init__(self, value): # Initialize our box. self.value = value def __bool__(self): # This returns true only if value is 1. if self.value == 1: return True else: return False # Create Box instances. # ... Use bool on them. box = Box(0) result = bool(box) print(result) box = Box(1) result = bool(box) print(result) box = Box(2) result = bool(box) print(result) Output False True False
Bool expressions. Often we can omit the "bool" keyword entirely—this can lead to clearer, shorter code. Here we store a bool in the correct and correct_bool variables.

Tip: In good Python code we focus on meaning, not lower-level constructs. The bool here is probably best left out.

Python program that uses bool expressions value = 5 # Both of these expressions evaluate to a bool. # ... The bool keyword is not required. correct = value == 5 correct_bool = bool(value == 5) # Print results. print(correct) print(correct_bool) Output True True
Argument examples. Let us be thorough with our examination of bool. When called with no arguments, bool() returns False. I have never used this in a Python program.
Python program that uses bool, no arguments # These evaluate to false. print(bool()) print(bool(None)) # The inner bool returns value. print(bool(bool())) # A string evaluates to true. print(bool("a")) Output False False False True
An int. A bool might look like a separate type. But it really is just an int—a subclass of an int. In the Python documentation (which is quite good) we find bool can be only True or False.

Quote: The bool class is a subclass of int. It cannot be subclassed further. Its only instances are False and True (see Boolean Values).

Built-in Functions: python.org
A review. Using bool() has been, in my experience, rare in simple Python programs. But it is important to know about it, for when it is needed.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls