novalue¶
- final class pythonic_fp.gadgets.sentinels.novalue.NoValue¶
Bases:
objectmissing value
Singleton class representing an actual, not potential, missing value.
While
Noneand()are frequently used as sentinel values, I prefer to think of them asNoneas returns, or returned, no values.()as an empty, possibly typed, iterable collection.
Important
Given variables
x: int | NoValue y: int | NoValue
Equality between
xandymeans both values exist and compare as equal.x == y¶y = NoValue()y = 42y = 57x = NoValue()FalseFalseFalsex = 42FalseTrueFalsex = 57FalseFalseTruex != y¶y = NoValue()y = 42y = 57x = NoValue()FalseFalseFalsex = 42FalseFalseTruex = 57FalseTrueFalseWarning
use
==or!=only in value comparisonsuse
isandis notto identity theNoValue()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
@overloadto keep theNoValuetype out of documentation and IDEs.
- __reduce__() tuple[type[NoValue], tuple[()]]¶
reduce
Tell
pickleandcopyhow to reconstruct the sentinel: callSentinel(flavor)rather than rebuilding the object directly.reconstruction goes through
__new__, so the flavor cache is consultedunpickling returns the existing NoValue singleton, or creates and caches it in a fresh interpreter
copyanddeepcopyuse the same protocol, so they return the singleton unchanged
- returns:
The pair
(NoValue, ()), whichpicklecalls to rebuild the sentinel.
Important
Without this,
picklewould construct a duplicateNoValueoutside the flavor cache, andiscomparisons against it would silently fail.
- __hash__() int¶
hash
- returns:
The singleton’s unique integer hash value.
- __eq__(other: object) bool¶
Equality comparison
- param other:
The object to be compared.
- returns:
Falseeven if compared to itself.
Warning
non-standard comparison semantics
always returns
Falseif one or both values are missing, then what is there to compare?
- __repr__() str¶
repr string
- returns:
The string ‘NoValue()’.
- __bool__() bool¶
bool
Always falsy.
- returns:
False
- __ne__(other: object) bool¶
not equal
- returns:
False
Warning
non-standard comparison semantics
always returns
False