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 == b
means two non-missing values compare as equal
usage
import
Nada
and 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 usingis
andis 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
None
butwhile
None
represents “returned no values”NoValue()
represents the absence of a value
Usage
import NoValue
frompythonic-fp.fptools.singletons
and theneither use
NoValue()
directlyor define
_noValue: Final[NoValue] = NoValue()
don’t export it
compare using
is
andis not
not
==
or!=
None
means returned no values, soNone == None
makes 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
None
as a hidden sentinel valueallows
None
to be stored in data structuresallows end users to choose to use
None
or()
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
is
andis not
or==
and!=
the
Sentinel()
value always equals itselfand never equals anything else, especially other sentinel values
- Parameters:
sentinel_name (
str
)