True — Advanced Playground
Boolean literal representing true/1
Python Playground
class QueryResult:
def __init__(self, rows):
self.rows = rows
def __bool__(self):
return len(self.rows) > 0
def __len__(self):
return len(self.rows)
empty = QueryResult([])
full = QueryResult([{"id": 1}, {"id": 2}])
print(f"Empty result: {bool(empty)}")
print(f"Full result: {bool(full)}")
if full:
print(f"Got {len(full)} rows")
Output
Click "Run" to execute your code
Python calls __bool__ first; if not defined, falls back to __len__. If neither exists, the object is always truthy.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?