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.
- __invert__() TF_Bool¶
bitwise invert operator
Inverts the value of an TF_Bool.
- returns:
The inverted value of the SBool.
- __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_Bool instance. A distinct type from F_Bool.
- static __new__(cls, witness: object = None, flavor: Hashable = None) Self¶
new
- param witness:
Ignored parameter, a T_Bool or T_Bool subtype is always truthy.
- param flavor:
Ignored parameter, only one truthy “flavor” for each T_Bool subtype.
- returns:
The truthy T_Bool or T_Bool subtype singleton instance.
- __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_Bool instance. A distinct type from T_Bool.
- static __new__(cls, witness: object = None, flavor: Hashable = None) Self¶
new
- param witness:
Ignored parameter, an F_Bool or F_Bool subtype is always truthy.
- param flavor:
Ignored parameter, only one truthy “flavor” for each F_Bool subtype.
- returns:
The truthy F_Bool or F_Bool subtype singleton instance.
- __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.