Hi,
Following some discussion on the python-dev mailing list, I’d like to poll your preferences on how we define sentinel values for optional function arguments.
This is a minor detail, so ISTM most important that we reach a reasonable decision quickly, even if that decision is that nothing should be done.
To quickly recap: The question is how to implement a sentinel value for an optional function argument where None
isn’t suitable. There are many of these in the stdlib, with some recently added examples used in the dataclasses module and in traceback.print_exception()
. Many of these, especially those defined using the common SENTINEL = object()
idiom, suffer from some drawbacks:
- Unclear and long repr which makes functions’ signatures long and hard to read
- Impossible to create a strict type signature for, due to having no dedicated type
- Get a new, distinct object after pickling and unpickling
The last of the above (surviving pickling) can perhaps be considered unimportant; see additional discussion on this related bpo issue.
POLL
From now on, without necessarily changing existing stdlib code…
How should we define user-visible sentinel values for optional function arguments in the stdlib?
- Consistent use of an idiom: Minimal dedicated class
class _SENTINEL: pass SENTINEL = _SENTINEL()
- Consistent use of an idiom: Minimal dedicated class with custom repr
class _SENTINEL: def __repr__(self): return f'{self.__class__.__module__}.SENTINEL' # or: return '<SENTINEL>' SENTINEL = _SENTINEL()
- Consistent use of an idiom: a single-value enum
class _SENTINEL(Enum): SENTINEL = 'SENTINEL' SENTINEL = _SENTINEL.SENTINEL
- Consistent use of a new, dedicated sentinel factory / class / meta-class
from sentinels import make_sentinel SENTINEL = make_sentinel('SENTINEL')
- Consistent use of a new, dedicated sentinel factory / class / meta-class, also made publicly available in the stdlib
- Consistent use of
Ellipsis
(a.k.a....
) - Consistent use of a single, new, additional sentinel value (e.g.
Sentinel
) - Something else
- The status-quo is fine / there’s no need for consistency in this
0 voters