PEP 661 – Sentinel Values, accepted 5 years later
9 hours ago
- #python
- #best-practices
- #programming
- PEP 661 introduces a built-in 'sentinel()' class for creating unique placeholder values in Python.
- The sentinel objects have clear, customizable reprs and survive copying and pickling, unlike common idioms like 'object()'.
- They are intended for use as function argument defaults, return values for missing results, or other sentinel needs where None is invalid.
- Type checkers will treat these sentinels as specific types, enabling precise type annotations and narrowing via 'is' checks.
- The PEP also adds C API functions, 'PySentinel_New()' and 'PySentinel_Check()', for C extensions.
- The implementation was chosen to avoid drawbacks of existing idioms (e.g., verbose repr, pickling issues) and keep usage straightforward.