novalue

final class pythonic_fp.gadgets.sentinels.novalue.NoValue

Bases: object

missing value

Singleton class representing an actual, not potential, missing value.

While None and () are frequently used as sentinel values, I prefer to think of them as

  • None as returns, or returned, no values.

  • () as an empty, possibly typed, iterable collection.

Important

Given variables

x: int | NoValue
y: int | NoValue

Equality between x and y means both values exist and compare as equal.

x == y

y = NoValue()

y = 42

y = 57

x = NoValue()

False

False

False

x = 42

False

True

False

x = 57

False

False

True

x != y

y = NoValue()

y = 42

y = 57

x = NoValue()

False

False

False

x = 42

False

False

True

x = 57

False

True

False

Warning

  • use == or != only in value comparisons

  • use is and is not to identity the NoValue() singleton itself

Tip

Use in a union type when creating “optional” arguments to functions and methods.

To help ensure the abstraction stays a hidden implementation detail and does not leak out into user code,

  • Do not export the sentinel value.

    • A new reference can always be generated via NoValue().

  • Use @overload to keep the NoValue type out of documentation and IDEs.

static __new__(cls) NoValue

new

returns:

The NoValue() singleton instance.

__hash__() int

hash

returns:

The singleton’s unique integer hash value.

__repr__() str

repr string

returns:

The string ‘NoValue()’.

__bool__() bool

bool

Always falsy.

returns:

False

__eq__(other: object) bool

Equality comparison

param other:

The object to be compared.

returns:

False even if compared to itself.

Warning

  • non-standard comparison semantics

  • always returns False

  • if one or both values are missing, then what is there to compare?

__ne__(other: object) bool

not equal

returns:

False

Warning

  • non-standard comparison semantics

  • always returns False