PEP 661: Sentinel values, has been accepted 5 years later
PEP 661, which introduces a built-in class for defining sentinel values in Python, has been accepted and marked as final for Python 3.15. The proposal aims to address limitations in existing sentinel value implementations, such as uninformative repr outputs and issues with copying or pickling. The new sentinel() built-in class and PySentinel_New() C API function will standardize sentinel usage in the standard library and user code.
- ▪PEP 661 proposes a built-in class for defining sentinel values to improve consistency and usability in Python.
- ▪Existing sentinel implementations, such as using object(), suffer from issues like unhelpful repr outputs and incorrect behavior after copying or pickling.
- ▪The PEP has been accepted and is targeted for inclusion in Python 3.15, with both Python and C API support provided.
- ▪Sentinel values are used in cases where None is insufficient, such as distinguishing missing arguments or representing unavailable data.
- ▪The change is not mandatory for existing standard library code, leaving adoption to module maintainers.
- ▪The proposal emerged from discussions in 2021 about improving sentinel usage in traceback.print_exception and other stdlib components.
Opening excerpt (first ~120 words) tap to expand
PEP 661 – Sentinel Values PEP 661 – Sentinel Values Author: Tal Einat <tal at python.org>, Jelle Zijlstra <jelle.zijlstra at gmail.com> Discussions-To: Discourse thread Status: Final Type: Standards Track Created: 06-Jun-2021 Python-Version: 3.15 Post-History: 20-May-2021, 06-Jun-2021 Resolution: 23-Apr-2026 Table of Contents Abstract Motivation Rationale Specification Typing C API Backwards Compatibility How to Teach This Security Implications Reference Implementation Rejected Ideas Use NotGiven = object() Add a single new sentinel value, such as MISSING or Sentinel Use the existing Ellipsis sentinel value Use a single-valued enum A sentinel class decorator Using class objects Define a recommended “standard” idiom, without supplying an implementation Use a new standard library module Use…
Excerpt limited to ~120 words for fair-use compliance. The full article is at Python Enhancement Proposals (PEPs).