Python 3 reference · Built-in Functions
Python sentinel() Function
Create a unique, named marker value (new in Python 3.15), such as MISSING, to tell 'no value given' apart from None.
Python 3 Since 3.15
Syntax
sentinel(name, /, *, repr=None)
Parameters
| Parameter | Description |
|---|---|
name |
The sentinel's name; also its default representation. |
repr optional |
A custom representation string. Default: None |
Return value
A new sentinel object, equal only to itself.
Examples
MISSING = sentinel("MISSING")
def get_setting(settings, key, default=MISSING):
if key in settings:
return settings[key]
if default is MISSING:
raise KeyError(key)
return default # None is a valid default here
print(get_setting({"debug": False}, "debug"))
print(get_setting({}, "theme", None))
print(MISSING)
Output
False None MISSING
Run this on your computer — see “Get Started” for setup.
MISSING = sentinel("MISSING")
print(MISSING is MISSING, MISSING == sentinel("MISSING"))
print(repr(MISSING), bool(MISSING))
Output
True False MISSING True
Run this on your computer — see “Get Started” for setup.
Good to know
- Compare sentinels with
is. - Assign the sentinel to a module-level variable with the same name (
MISSING = sentinel("MISSING")): copy and pickle find it again by that name. - Before Python 3.15, use
MISSING = object()(seeobject()).
Version history
| Version | Change |
|---|---|
| Python 3.15 | Added |
Related
object()· Return a new featureless object. object is the base class of every Python class and provides the default methods.- Python Function Arguments · tutorial