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

ParameterDescription
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

Distinguish 'not given' from None
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.

Each sentinel is unique
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() (see object()).

Version history

VersionChange
Python 3.15Added