
On Sat, May 15, 2021 at 2:04 AM Barry Warsaw <barry@python.org> wrote:
On May 14, 2021, at 02:38, Chris Angelico <rosuav@gmail.com> wrote:
Do we ever really need the ability to pass a specific sentinel to a function, or are we actually looking for a way to say "and don't pass this argument”?
Very often, that’s the case. Such a “it’s okay to not pass this argument” construct would have to work with the Optional type too.
What I mean is: how often do you actually need to pass the specific sentinel, as opposed to the normal construct of asking if the argument was or wasn't passed? Eg if you have a function like this: _sentinel = object() def func(x, y=_sentinel): if y is _sentinel: ... else: ... Would you ever call it like this: func(42, _sentinel) ? Because if there's no reason to ever pass the sentinel, then it's nothing more than an implementation detail for the concept of "was this argument passed?", and that's exactly what I'm asking about.
The other use case I have for a special case single use singleton is for dict.get(), i.e.
missing = object() value = somedict.get(‘key’, missing) if value is missing: # It ain’t there.
I'd write that one with a try/except instead. The whole point of get() is to provide the missing value. try: value = somedict['key'] except KeyError: # It ain't there. No sentinel needed. Same number of lines. ChrisA