AI Skill Report Card
Evaluating Python Truthiness
Quick Start13 / 15
Python evaluates any object's truth value using bool(obj). By default, everything is truthy except a specific set of built-in falsy values:
Python# Falsy values False, None, 0, 0.0, 0j, Decimal(0), Fraction(0, 1) '', (), [], {}, set(), frozenset(), range(0) # Everything else is truthy if []: # False - empty list if [0]: # True - non-empty list (even containing 0) if "0": # True - non-empty string
Recommendation▾
Add an example involving inheritance (e.g., subclass overriding __bool__ vs __len__) to cover more nuanced edge cases.
Workflow12 / 15
-
Identify the object's type category:
- Numeric type → falsy if equal to zero
- Container/sequence → falsy if
len(obj) == 0 - Custom class → check for
__bool__, then__len__, then defaultTrue
-
Determine which dunder method controls truthiness:
Progress: - [ ] Does the class define __bool__? → its return value (must be bool) wins - [ ] No __bool__, but defines __len__? → truthy iff len(obj) != 0 - [ ] Neither defined? → object is always truthy -
Validate
__bool__implementation — it must return an actualbool; returning something else raisesTypeError.
Recommendation▾
Include a real-world debugging scenario example, such as a custom class unexpectedly evaluating as falsy in production code, to demonstrate practical application.
Examples15 / 20
Example 1:
Input: bool(0.0)
Output: False — numeric zero of any numeric type is falsy.
Example 2: Input:
Pythonclass Bucket: def __len__(self): return 0 bool(Bucket())
Output: False — no __bool__ defined, falls back to __len__, which returns 0.
Example 3: Input:
Pythonclass AlwaysTrue: pass bool(AlwaysTrue())
Output: True — no __bool__ or __len__, default object truthiness applies.
Example 4: Input:
Pythonclass Weird: def __bool__(self): return "yes" bool(Weird())
Output: TypeError: __bool__ should return bool, returned str
Recommendation▾
The workflow's step 2 checklist format is slightly odd for a linear decision process—consider a simple flowchart or ordered decision tree instead of checkboxes since these aren't independently completable tasks.
Best Practices
- Prefer implicit truthiness checks (
if items:) over explicit comparisons (if len(items) > 0:) for containers — it's idiomatic and handles all falsy cases uniformly. - When designing custom classes meant to be used in boolean contexts, implement
__bool__explicitly rather than relying on__len__inference if the semantics aren't literally "does it have length." - Use
is None/is not Noneinstead of truthiness when you specifically care aboutNonevs. other falsy values (e.g.,0or""are valid but falsy).
Common Pitfalls
- Confusing
0,"",[], andNonein conditionals —if x:treats all of these asFalse; use explicit checks (if x is None,if x == 0) when the distinction matters. - Forgetting that non-empty containers with falsy contents are still truthy —
if [0]:isTruebecause the list has one element, regardless of that element's own truthiness. - Returning non-bool from
__bool__— this raisesTypeError, unlike__len__, which just needs to return a non-negative integer. - Assuming NaN is falsy —
bool(float('nan'))isTrue; only actual zero values are falsy for numerics.