CKTIS / Python
bool()
Converts the truth value of an object into a Boolean.
Version: Python 3.14; repository image, production runtime not attested
Syntax
bool(object=False, /)Converts the truth value of an object into a Boolean.
A nonempty string tests true even when it spells the word false. An empty container tests false.
Truth testing first uses __bool__, or __len__ when no Boolean hook is defined. An object with neither is normally true. The nonempty string "False" is true regardless of its spelling, while the empty list gives false. With no argument bool() returns False.
assert not bool() and not bool(0) and bool("False")
class Empty:
def __len__(self): return 0
assert not bool(Empty())
Examples
print(bool("False"))
print(bool([]))
Result
True False
Truth testing first uses `__bool__`, or `__len__` when no Boolean hook is defined. An object with neither is normally true. The nonempty string "False" is true regardless of its spelling, while the empty list gives false. With no argument bool() returns False.