Module singletons¶
Pythonic FP - Collection of singleton classes
- final class pythonic_fp.fptools.singletons.Nada¶
Singleton class representing & propagating failure.
singleton
_nada: nada = Nada()represents a non-existent valuereturns itself for arbitrary method calls
returns itself if called as a Callable with arbitrary arguments
interpreted as an empty container by standard Python functions
warning: non-standard equality semantics
comparison compares true only when 2 non-missing values compare true
thus
a == bmeans two non-missing values compare as equal
usage
import
Nadaand theneither use
Nada()directlyor define
_nada: Final[Nada] = Nada()don’t export it
start propagating failure by setting a propagating value to Nada()
works best when working with expression
failure may fail to propagate
for a function/method with just side effects
engineer Nada() to fail to trigger side effects
test for failure by comparing a result to
Nada()itself usingisandis not
propagate failure through a calculation using
==and!=the
Nada()value never equals itselfand never equals anything else
- nada_get(alt=Sentinel('Nada'))¶
Get an alternate value, defaults to
Nada().- Parameters:
alt (
Any)- Return type:
Any
- class pythonic_fp.fptools.singletons.NoValue¶
Singleton class representing a missing value.
similar to
Nonebutwhile
Nonerepresents “returned no values”NoValue()represents the absence of a value
Usage
import NoValuefrompythonic-fp.fptools.singletonsand theneither use
NoValue()directlyor define
_noValue: Final[NoValue] = NoValue()don’t export it
compare using
isandis notnot
==or!=Nonemeans returned no values, soNone == Nonemakes senseif one or both values are missing, then what is there to compare?
- final class pythonic_fp.fptools.singletons.Sentinel(sentinel_name)¶
Singleton classes representing a sentinel values.
intended for library code, not to be exported/shared between modules
otherwise some of its intended typing guarantees may be lost
useful substitute for
Noneas a hidden sentinel valueallows
Noneto be stored in data structuresallows end users to choose to use
Noneor()as sentinel valuesalways equals itself (unlike
NoValue)
usage
import Sentinel and then either
define
_my_sentinel: Final[Sentinel] = Sentinel('my_sentinel')or use
Sentinel('my_sentinel')directly
compare using either
isandis notor==and!=the
Sentinel()value always equals itselfand never equals anything else, especially other sentinel values
- Parameters:
sentinel_name (
str)