flavored

Sentinel values “indexed” by hashable values of a given type.

final class pythonic_fp.gadgets.sentinels.flavored.Sentinel

Bases: Generic

Sentinel

Sentinel values labeled by different (hashable) flavors.

Note

  • Useful for union types.

  • A flavored Sentinel value always equals itself and never equals anything else, especially other flavored sentinel values.

static __new__(cls, flavor: H) → Sentinel

new

param flavor:

Hashable value determining which flavored Sentinel to return.

returns:

The Sentinel(flavor) singleton instance.

__reduce__() → tuple[type[Sentinel], tuple[H]]

reduce

Tell pickle and copy how to reconstruct the sentinel: call Sentinel(flavor) rather than rebuilding the object directly.

  • reconstruction goes through __new__, so the flavor cache is consulted

  • unpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter

  • copy and deepcopy use the same protocol, so they return the singleton unchanged

returns:

The pair (Sentinel, (flavor,)), which pickle calls to rebuild the sentinel.

Important

Without this, pickle would construct a duplicate Sentinel outside the flavor cache, and is comparisons against it would silently fail.

__repr__() → str

repr string

Construct string ‘Sentinel(flavor)’ where the flavor is displayed with repr().

returns:

A string to reproduce the flavored sentinel.

__str__() → str

user string

Construct string ‘Sentinel(flavor)’ where the flavor is displayed with str().

returns:

A string meaningful to an end user.

flavor() → H

get flavor

returns:

The sentinel’s flavor.

__annotate_func__()

The type of the None singleton.