bool()
The built-in bool()
function allows you to determine the truth value of any Python object. It returns True
or False
, based on whether the object is considered truthy or falsy in Python:
>>> bool(0)
False
>>> bool(42)
True
bool()
Signature
bool([x])
Arguments
Argument | Description | Default Value |
---|---|---|
x |
The object to be checked. | False |
Return Value
- Returns the truth value of any Python object as a Boolean value (
True
orFalse
).
bool()
Examples
With no argument:
>>> bool()
False
With falsy objects:
>>> # Built-in constants
>>> bool(None)
False
>>> bool(False)
False
>>> # Numeric zeros
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(0j)
False
>>> # Empty sequences
>>> bool("")
False
>>> bool([])
False
>>> bool(())
False
>>> bool(range(0))
False
>>> # Empty dictionary and set
>>> bool({})
False
>>> bool(set())
False
With numbers as arguments:
>>> bool(42)
True
>>> bool(3.14)
True
With a string as an argument:
>>> bool("Hello")
True
bool()
Common Use Cases
The most common use cases for the bool()
function include:
- Evaluating the truthiness of variables or expressions
- Normalize objects to Boolean values in conditional expressions
- Implementing custom Boolean logic in classes
bool()
Real-World Example
As an example of using bool()
, say that you need to implement a stack data structure that should be cosidered falsy if it’s empty and truthy otherwise. You can do the following:
stack.py
class Stack:
def __init__(self, items=None):
self.items = list(items) if items is not None else []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def __bool__(self):
return bool(self.items)
# Usage
stack = Stack()
print(bool(stack)) # Output: False
stack.push(4)
print(bool(stack)) # Output: True
In this example, your Stack
class implements the .__bool__()
special method to support Boolean operations on its objects. This method guarantees that when a given Stack
object is empty, the bool()
function returns False
and True
otherwise.
bool()
in Custom Classes
As you’ve seen. you can support the bool()
function in custom classes by defining the .__bool__()
special method. Here’s a quick example:
container.py
class Container:
def __init__(self, items=None):
self.items = items or []
def __bool__(self):
return len(self.items) > 0
# Usage
container = Container()
print(bool(container)) # Output: False
container.items.append(1)
print(bool(container)) # Output: True
In this example, the Container
class uses the .__bool__()
method to determine its truthiness based on whether it contains any items.
Related Resources
Tutorial
Python Booleans: Use Truth Values in Your Code
In this tutorial, you'll learn about the built-in Python Boolean data type, which is used to represent the truth value of an expression. You'll see how to use Booleans to compare values, check for identity and membership, and control the flow of your programs with conditionals.
For additional information on related topics, take a look at the following resources:
- How to Implement a Python Stack (Tutorial)
- Basic Data Types in Python: A Quick Exploration (Tutorial)
- Python Booleans: Leveraging the Values of Truth (Course)
- Implementing a Stack in Python (Course)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)