Truthy Falsy Booleans¶
Truthy and Falsy are not just different values, but different classes.
- class pythonic_fp.booleans.truthy_falsy.TF_Bool¶
Bases:
SBoolTruthy-Falsy Booleans
A Boolean implementation whose distinct truthy and falsy values are distinct singleton subclasses, not just distinct singleton values.
Important
Do not subclass
TF_Boolitself. Subclassing it is designed to fail at runtime.Tip
Logically combine these using Python’s bitwise operators.
Warning
Although these datastructures are completely compatible with Python shortcut evaluation, care needs to be taken when using them with the
and,or, andnotbuiltins.For example:
~ALWAYS is NEVERnot ALWAYS is False(ALWAYS and False) is False(ALWAYS or False) is ALWAYS
- static __new__(cls, witness: object, flavor: Hashable = None) Self¶
__new__
- param witness:
Determines which subtype,
T_BoolorF_Bool, ofTF_Boolto return.- param flavor:
Ignored parameter, only two flavors, one truthy and one falsy.
- returns:
The singleton truthy or singleton falsy instance.
- classmethod __init_subclass__(**kwargs: object) None¶
__init_subclass__
- raises TypeError:
Fails at runtime if an attempt to subclass outside this module is made.
- __invert__() Self¶
bitwise invert operator
Inverts the value of an TF_Bool.
- returns:
The inverted value of the TF_Bool.
- __repr__() str¶
repr string
A string to reproduce the TF_Bool.
- returns:
‘T_Bool()’ if truthy, otherwise F_Bool()
- __str__() str¶
user string
A string meaningful to an end user.
- returns:
‘ALWAYS’ if truthy, otherwise ‘NEVER’
- final class pythonic_fp.booleans.truthy_falsy.T_Bool¶
Bases:
TF_BoolTruthy TF_Bool subclass
Type of the truthy singleton
TF_Boolinstance. A distinct type fromF_Bool.- static __new__(cls, witness: object = None, flavor: Hashable = None) Self¶
new
- param witness:
Ignored parameter, a T_Bool is always truthy.
- param flavor:
Ignored parameter, only one truthy “flavor”
- returns:
The truthy T_Bool singleton instance.
- classmethod __init_subclass__(**kwargs: object) None¶
__init_subclass__
- raises TypeError:
Fails at runtime if an attempt to subclass outside this module is made.
- __reduce__() tuple[type[T_Bool], tuple[object]]¶
reduce
Tell
pickleandcopyhow to reconstruct theT_Boolsingleton by callingT_Bool()rather than rebuilding the object directly.reconstruction goes through
__new__, so the flavor cache is consultedunpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter
copyanddeepcopyuse the same protocol, so they return the singleton unchanged
- returns:
The pair
(TBool, (None,)), whichpickleuses to rebuild the singleton. TheNoneargument is ignored and is there only to satisfy the Liskov substitution principle.
Important
Without this,
picklewould construct a duplicateT_Booloutside the instancedict, andiscomparisons against it would silently fail.
- __and__(other: int) int¶
bitwise and operator
Overloading
&to perform Boolean “and” ifotheris anotherTF_Bool.- param other:
Any object derived from int.
- returns:
Boolean “and” if other is an SBool otherwise returns bitwise “and”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.
- __or__(other: int) int¶
bitwise or operator
Overloading
|to perform Boolean “or” ifotheris anotherTF_Bool.- param other:
Any object derived from int.
- returns:
Boolean “or” if other is an SBool otherwise returns bitwise “or”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.
- __xor__(other: int) int¶
bitwise xor operator
Overloading
^to perform Boolean “xor” ifotheris anotherTF_Bool.- param other:
Any object derived from int.
- returns:
Boolean “xor” if other is an SBool otherwise returns bitwise “xor”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.
- final class pythonic_fp.booleans.truthy_falsy.F_Bool¶
Bases:
TF_BoolFalsy TF_Bool subclass
Type of the falsy singleton
TF_Boolinstance. A distinct type fromT_Bool.- static __new__(cls, witness: object = None, flavor: Hashable = None) Self¶
new
- param witness:
Ignored parameter, an F_Bool is always falsy.
- param flavor:
Ignored parameter, only one falsy “flavor”.
- returns:
The falsy F_Bool singleton instance.
- classmethod __init_subclass__(**kwargs: object) None¶
__init_subclass__
- raises TypeError:
Fails at runtime if an attempt to subclass outside this module is made.
- __reduce__() tuple[type[F_Bool], tuple[object]]¶
reduce
Tell
pickleandcopyhow to reconstruct theF_Boolsingleton by callingF_Bool()rather than rebuilding the object directly.reconstruction goes through
__new__, so the flavor cache is consultedunpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter
copyanddeepcopyuse the same protocol, so they return the singleton unchanged
- returns:
The pair
(FBool, (None,)), whichpicklecalls to rebuild the sentinel. The argument is ignored and is there only to satisfy the Liskov substitution principle.
Important
Without this,
picklewould construct a duplicateF_Booloutside the instancedict, andiscomparisons against it would silently fail.
- __and__(other: int) int¶
bitwise and operator
Overloading
&to perform Boolean “and” ifotheris anotherSBool.- param other:
Any object derived from int.
- returns:
Boolean “and” if other is an SBool otherwise returns bitwise “and”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.
- __or__(other: int) int¶
bitwise or operator
Overloading
|to perform Boolean “or” ifotheris anotherSBool.- param other:
Any object derived from int.
- returns:
Boolean “and” if other is an SBool otherwise returns bitwise “and”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.
- __xor__(other: int) int¶
bitwise xor operator
Overloading
^to perform Boolean “xor” ifotheris anotherSBool.- param other:
Any object derived from int.
- returns:
Boolean “and” if other is an SBool otherwise returns bitwise “and”.
- rtype:
First common ancestor of the types of self and other if both of type SBool, otherwise int.