The Python Steering Council recently discussed PEP 661, Sentinel Values. This post is on behalf of the SC.
Weâd like to thank Tal for writing this PEP and leading a fruitful discussion on DPO. Although we are not yet pronouncing on the PEP, the SC is generally supportive of the idea of adding official sentinel values to Python.
As the use of object() for unique sentinels shows, the motivation for sentinels is clear, and we agree with the rationale provided in the PEP. We think that sentinels should be at least as easy to use as object(), while providing clear benefits over that simple and existing idiom, and we agree the implementation should be as simple as possible to achieve these goals.
Here is some additional feedback on the PEP. Weâre not implying that acceptance of the PEP is conditional on accepting our recommendations, but we want to give you a clear idea of what weâre thinking, and weâd like to encourage you to take advantage of Steering Council office hours if youâd like to discuss our feedback in person.
The PEP proposes that sentinels within the same module are unique, and implements a global registry to ensure this. We think that this is âmagicalâ behavior that isnât supported strongly enough by the rationale and motivations in the PEP. Why is this implicit behavior necessary? Wouldnât defining a module global sentinel explicitly and then referring to it by variable name be sufficient? We suggest that removing this behavior would keep the implementation simpler while still achieving the goals of the PEP.
Along those same lines, the PEP proposes to auto-discover the module that the sentinel is defined in, while still providing an optional module_name argument to the constructor for cases where the auto-discovery canât or doesnât work. While this parallels the design of Enum and namedtuple, we arenât convinced that this is useful for sentinels, especially if the global registry feature is removed as suggested above. We think a single name argument, which would be used directly in the repr, should be sufficient to achieve the goals of the PEP. If the user wanted to include the module name in that argument, itâs easy enough to do so explicitly.
We think itâs fine for sentinels to evaluate as truthy in boolean contexts.
The PEP proposes a new module called sentinellib which will contain a Sentinel class. We note however that in the section titled âTypingâ, the examples use a module called sentinels. In either case, we think that to improve adoption by making sentinels as easy to use as the current object() idiom, a builtin type / callable called sentinel should be provided. With a simplified implementation, it may not even be necessary to implement sentinels as Python code living in a new standard library module (e.g. it could be implemented in C).
Thanks again Tal, and weâre looking forward to further discussions.