GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python __bool__ Method - Complete Guide Python __bool__ MethodLast modified April 8, 2025
This comprehensive guide explores Python's Basic Definitions
The
When Basic __bool__ Implementation
Here's a simple example showing how to implement basic_bool.py
class Account:
def __init__(self, balance):
self.balance = balance
def __bool__(self):
return self.balance > 0
account1 = Account(100)
account2 = Account(-50)
print(bool(account1)) # True
print(bool(account2)) # False
if account1:
print("Account has funds")
This example defines an
The method is automatically called when the object is used in a boolean
context, like __bool__ with __len__ Fallback
When bool_len.py
class ShoppingCart:
def __init__(self, items):
self.items = items
def __len__(self):
return len(self.items)
def __bool__(self):
return any(item.price > 0 for item in self.items)
class Item:
def __init__(self, price):
self.price = price
cart1 = ShoppingCart([Item(10), Item(20)])
cart2 = ShoppingCart([Item(0), Item(0)])
print(bool(cart1)) # True (__bool__ returns True)
print(bool(cart2)) # False (__bool__ returns False)
print(bool(ShoppingCart([]))) # False (__len__ returns 0)
This example shows that The cart is considered truthy only if it contains at least one item with a positive price, demonstrating custom truthiness logic. Truthiness in Data Validation
validation.py
class UserProfile:
def __init__(self, name, email):
self.name = name
self.email = email
self._validate()
def _validate(self):
self.is_valid = (
isinstance(self.name, str) and
'@' in self.email
)
def __bool__(self):
return self.is_valid
valid_user = UserProfile("Alice", "[email protected]")
invalid_user = UserProfile(123, "bobexample.com")
print(bool(valid_user)) # True
print(bool(invalid_user)) # False
if not invalid_user:
print("Profile is invalid")
This example uses This pattern is useful when you want objects to self-report their validity in a natural way through boolean testing. Custom Container Truthiness
For container-like objects, container.py
class Playlist:
def __init__(self, songs):
self.songs = list(songs)
def __bool__(self):
return len(self.songs) > 0 and any(
song.duration > 0 for song in self.songs
)
def __len__(self):
return len(self.songs)
class Song:
def __init__(self, duration):
self.duration = duration
empty_playlist = Playlist([])
playlist_with_silent_tracks = Playlist([Song(0), Song(0)])
valid_playlist = Playlist([Song(180), Song(240)])
print(bool(empty_playlist)) # False
print(bool(playlist_with_silent_tracks)) # False
print(bool(valid_playlist)) # True
This The implementation follows Python's principle that empty containers are falsey, but extends it with domain-specific logic about what constitutes a "valid" playlist. __bool__ in Stateful Objects
For stateful objects, stateful.py
class TrafficLight:
def __init__(self):
self._state = 'red'
def change(self):
if self._state == 'red':
self._state = 'green'
elif self._state == 'green':
self._state = 'yellow'
else:
self._state = 'red'
def __bool__(self):
return self._state == 'green'
light = TrafficLight()
print(bool(light)) # False
light.change()
print(bool(light)) # True
light.change()
print(bool(light)) # False
This traffic light implementation uses
This pattern makes code more readable when checking object state, as
Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |