AI Skill Report Card

Explaining Python Builtin Constants

B-66·Aug 12, 2026·Source: Web
13 / 15
Python
# Correct singleton comparisons: use `is`, not `==` if value is None: ... # NotImplemented: return from special methods when operation unsupported class Vector: def __eq__(self, other): if not isinstance(other, Vector): return NotImplemented return self.x == other.x and self.y == other.y # Ellipsis: placeholder in stubs/slices def stub_function() -> int: ... matrix[..., 0] # __debug__: guards code stripped in optimized mode (-O) if __debug__: assert expensive_check(), "This block vanishes under python -O"
Recommendation
This is reference/explanatory documentation rather than a task-oriented skill workflow — consider framing it around a concrete task (e.g., 'auditing code for constant misuse') with clearer input/output before-and-after diffs.
12 / 15
  1. Identify which constant applies to the situation:

    • True/False — boolean values (instances of int subclass bool)
    • None — absence of a value (the sole instance of NoneType)
    • NotImplemented — signal to Python that a binary operator/comparison isn't supported for these types
    • Ellipsis (...) — placeholder (slices, stubs, unimplemented bodies)
    • __debug__True unless run with -O/-OO; controls assert execution
  2. Use identity checks (is/is not) for singletons — never == for None, True, False, NotImplemented, Ellipsis. These are guaranteed singletons; identity comparison is faster and semantically correct.

  3. For operator overloading, return NotImplemented (not raise an exception, not return False) when your type can't handle the other operand. Python then tries the reflected method or raises TypeError itself.

  4. Never evaluate NotImplemented in a boolean context directly — it's truthy and doing if x == y: where __eq__ might return NotImplemented can silently produce wrong results if not handled by Python's dispatch. Only return it from special methods; don't manually branch on it in general code.

  5. For __debug__-guarded code, remember it's a compile-time constant — the if __debug__: block is literally removed from bytecode under -O. Don't put code with side effects required for correctness inside it (same rule as assert).

Recommendation
Examples are somewhat abstract (single-line scenarios) rather than full realistic code snippets with actual buggy code shown alongside the fix side-by-side.
13 / 20

Example 1: Input: Reviewing code with if x == None: Output: Flag and rewrite as if x is None:== can be overridden by __eq__ and is semantically wrong for singleton identity checks; is is also faster.

Example 2: Input: Implementing __lt__ for a custom class comparing against arbitrary types Output:

Python
def __lt__(self, other): if not isinstance(other, MyClass): return NotImplemented return self.value < other.value

Example 3: Input: Writing a .pyi stub file for a function body Output:

Python
def connect(host: str, port: int) -> Connection: ...

... (Ellipsis) is the idiomatic stub body placeholder, distinct from pass.

Example 4: Input: assert user.is_authenticated(), "must be logged in" used to enforce security Output: Flag as a bug — under python -O, __debug__ is False and all assert statements are compiled out, so this check silently disappears. Use an explicit if not ...: raise PermissionError(...) instead.

Recommendation
Add a bad-vs-good example pairing explicitly (e.g., show the wrong code and the corrected code together) to strengthen the examples section.
  • Treat True/False as int subtypes when relevant (True == 1, isinstance(True, int) is True) but don't rely on this for readability — prefer explicit booleans in conditionals.
  • Use None as the default sentinel for "no value provided" in function signatures, but if None is a valid meaningful argument, use a dedicated sentinel object instead.
  • Document custom classes' __eq__/__lt__/etc. as returning NotImplemented for unsupported types so subclasses and reflected operations behave correctly.
  • Use Ellipsis in NumPy-style slicing (arr[..., 0]) and in type-stub/interface bodies; avoid using it as a generic "TODO" placeholder in production logic (use raise NotImplementedError instead — different from NotImplemented).
  • Confusing NotImplemented with NotImplementedError. NotImplemented is a constant returned from special methods; NotImplementedError is an exception raised in abstract method bodies. They are not interchangeable.
  • Using == instead of is for None/singletons — fragile if __eq__ is overridden.
  • Putting required side effects inside if __debug__: or assert — vanishes under -O.
  • Returning False instead of NotImplemented from comparison dunders when the type doesn't match — this incorrectly asserts inequality/falsity rather than deferring to the other operand's reflected method.
  • Using Ellipsis as an empty function body placeholder in real (non-stub) code where pass or a real implementation is expected — reserve ... for stubs/interfaces.
0
Grade B-AI Skill Framework
Scorecard
Criteria Breakdown
Quick Start
13/15
Workflow
12/15
Examples
13/20
Completeness
15/20
Format
13/15
Conciseness
13/15