sentinels.novalue¶
Singleton class representing a missing value.
In untyped Python, both None
and ()
are often used as sentinel values.
When typing tools like MyPy are used, both are shoe-horded into Union types.
NoValue()
is a singleton object representing a missing value.
Given variables
x: int | NoValue
y: int | NoValue
Equality between x
and y
means both values exist and compare as equal.
If one or both of theses values are missing, then what is there to compare?
x∖y |
NoValue() |
42 |
57 |
---|---|---|---|
NoValue() |
false |
false |
false |
42 |
false |
true |
false |
57 |
false |
false |
true |
x∖y |
NoValue() |
42 |
57 |
---|---|---|---|
NoValue() |
false |
false |
false |
42 |
false |
false |
true |
57 |
false |
true |
false |
Of course, we can also compare directly by identity.
if x is NoValue():
print('nobody home')
...
Warning
Threadsafe only if instantiated before going multi-threaded.
Warning
Non-standard (unpythonic?) ==
and !=
comparison operators.