Open legendof-selda opened 1 year ago
Hi @legendof-selda,
Thank you for your thoughtful suggestions!
My apologies for not replying thus far. (I have not been able to do nearly any work on open-source things in the past year+.)
Regarding your points:
Naming Conventions: Since the PEP aims to add a general solution for sentinel objects, which could be used in various use cases, I think that prescribing a naming convention would be counter-productive. I prefer to leave the naming to be done according to general naming conventions, and/or the conventions of the relevant code base, for better consistency. E.g., if a code base uses ALL_CAPS for constants, it may make sense for a constant sentinel value to use such naming.
Avoiding Conflicts: This is important, but I see nothing special about sentinels in this regard; this is true for all variables and is already covered by existing style guides.
Usage of Sentinels: Indeed, this should likely be included in the docs if and when this is added to the Python stdlib. (This is already clearly outlined in the rationale of the PEP.)
Complexity of Sentinels: This is a nice idea; I'll consider including this as a recommendation.
Consistency: Similar to point 2 above, this is a good general recommendation, but I can't think of a strong reason to point this out especially for sentinels.
Sentinel Comparison Order: Good point! I'll certainly include this in the docs, and perhaps add it to the PEP as well.
Explicit Boolean Evaluation: Making boolean evaluation (i.e. "truthiness checks") raise an exception by default would be too big of a deviation from existing common behavior: This almost never happens in Python. Therefore, I feel strongly that, by default, sentinels should evaluate to either True or False in a boolean context. Evaluating to True for sentinels other than None has a precedence: bool(Ellipsis)
results in True.
I hope to complete this PEP soon, and welcome further discussion in the near future if you're interested.
I propose a style guide when we create a
sentinel
. Common standard python sentinels like 'True', 'False', 'None' follow this styleUndefined
,Missing
.None
sentinel when it fits the purpose as this is normal python standard.Enums
instead.Explicit Boolean Evaluation: Provide Boolean evaluation explicitly rather than setting it to be
True
by default as this avoids confusion.Example:
Here you can see that, it makes more sense that Undefined was
False
rather than it beingTrue
by default. If a sentinel must beTrue
orFalse
depends on the context of the sentinel. Example:Undefined
should be evaluated toFalse
andSuccess
can be evaluated toTrue
, whileEOF
doesn't necessarily meanTrue
orFalse
and thus can have an "Invalid State" or it is "ambiguous". So, I propose that we should pass in explicitly if a Sentinel is truthy or not, using thetruthy
argument. Thetruthy
argument can beTrue
,False
orNone
(default). Iftruthy
isNone
, then we should raise aValueError
. This makes sentinels more explicit and can force the users to give meaning to a sentinel and enforces them to useis
operator to evaluate.Example:
This makes the behavior of sentinels more explicit and avoids any ambiguity.
Can we discuss this and possibly add it into PEP 661 Proposal?