AI Skill Report Card

Evaluating Python Truthiness

B+78·Aug 12, 2026·Source: Web
13 / 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.
12 / 15
  1. 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 default True
  2. 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
    
  3. Validate __bool__ implementation — it must return an actual bool; returning something else raises TypeError.

Recommendation
Include a real-world debugging scenario example, such as a custom class unexpectedly evaluating as falsy in production code, to demonstrate practical application.
15 / 20

Example 1: Input: bool(0.0) Output: False — numeric zero of any numeric type is falsy.

Example 2: Input:

Python
class Bucket: def __len__(self): return 0 bool(Bucket())

Output: False — no __bool__ defined, falls back to __len__, which returns 0.

Example 3: Input:

Python
class AlwaysTrue: pass bool(AlwaysTrue())

Output: True — no __bool__ or __len__, default object truthiness applies.

Example 4: Input:

Python
class 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.
  • 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 None instead of truthiness when you specifically care about None vs. other falsy values (e.g., 0 or "" are valid but falsy).
  • Confusing 0, "", [], and None in conditionalsif x: treats all of these as False; use explicit checks (if x is None, if x == 0) when the distinction matters.
  • Forgetting that non-empty containers with falsy contents are still truthyif [0]: is True because the list has one element, regardless of that element's own truthiness.
  • Returning non-bool from __bool__ — this raises TypeError, unlike __len__, which just needs to return a non-negative integer.
  • Assuming NaN is falsybool(float('nan')) is True; only actual zero values are falsy for numerics.
0
Grade B+AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
12/15
Examples
15/20
Completeness
15/20
Format
15/15
Conciseness
14/15