singletons.sbool¶
Class SBool - Subclassable Singleton Booleans
Python does not permit bool to be subclassed, but int
can be
subclassed. Under-the-hood a bool
is just an int
. The
SBool class inherits from int
and relies on the underlying
truthiness and falsiness of 1
and 0
.
The Truth(truth: str)
and Lie(lie: str)
subclass constructors
produce singletons based on their input parameters. When using type
hints, declare variables of these types as type SBool
.
Best practices when used with these subclasses are:
use == or != for pure Boolean comparisons
use is or not is if the type of truth matters
only use
SBool
as a type, never as a constructorwhen using Python shortcut logic remember
an instance of
Truth
is truthyan instance of
Lie
is falsyshortcut logic is lazy
the last truthy thing evaluated is returned
and is not converted to a
bool
the not statement converts a
SBool
to an actualbool
- class pythonic_fp.singletons.sbool.Lie(flavor: str = '')¶
Falsy singleton SBool subclass.
Note
When using type hints, declare variables SBool, not Lie.
- class pythonic_fp.singletons.sbool.SBool¶
Subclassable Boolean hierarchy.
This class’s sub-types represent different “flavors” of “truth” where each flavor has one unique “truthy” and one unique “falsy” instance.
Note
Python does not permit bool to be subclassed, but
int
can be. Under-the-hood abool
is just anint
. This class inherits fromint
and relies on the underlying truthiness and falsiness of1
and0
.Important
Only use SBool as a type, never as a constructor.
- class pythonic_fp.singletons.sbool.Truth(flavor: str = 'DEFAULT_TRUTH')¶
Truthy singleton SBool subclass.
Note
When using type hints, declare variables SBool, not Truth.