
On Sun, Sep 11, 2022 at 5:30 AM Stephen J. Turnbull <stephenjturnbull@gmail.com> wrote:
Christopher Barker writes:
Hmm -- so it's a semi-singleton -- i.e. behaves like a singlton, but only in a particular module namespace.
I don't see why this is a problem. Sentinels are "out of band" signals, they're invalid data *for the problem at hand*. That doesn't mean they're *always* invalid data. For example, Unicode surrogate characters are invalid in conforming streams of characters like Python str, but Python uses them internally anyway as "sentinels" meaning "byte value #XX encountered in input, but is invalid there". (You might not agree that these are sentinels, but I'm hard put to see why my interpretation is incorrect.) And, of course, Unicode itself uses them in concrete representations of characters, specifically UTF-16.
So sentinel usage is fundamentally private to an application as far as I can see, and the sentinel value is fundamentally arbitrary, as long as it's not valid data for the application.
Indeed! The PEP mentions -1 as return by str.find() as another example.
I would like to see a handful of common sentinels defined. Is there a plan to put a few that are useful for the stdlib in the `sentinel` module?
I don't think this works. You end up with the problem that None has as a sentinel: mostly it's great, but occasionally application's interpretation gets in the way of the sentinel semantics because in the end Python considers it a valid datum. For modules to be composable, they need to agree on the *exact* semantics of the sentinel. If a convention based on one of the true singletons (None, Ellipsis, True, False) doesn't work, you probably want a private sentinel anyway.
This mirrors much of my thinking. Adding an additional sentinel value for general use, similar to None and Ellipses, would be entirely separate from this proposal. The intention here is to allow every function, class or module which needs a sentinel value to define its own unique sentinel(s). I think this is better in most cases, since it makes things clear and well separated. (I arrived at this conclusion by going through many examples of existing code using sentinel values, both in the stdlib and elsewhere.) - Tal