Subtypable Boolean¶
Like the Python builtin type bool, but subtypable.
- class pythonic_fp.booleans.subtypable.SBool¶
Bases:
intSubtypable Boolean
Like Python’s built in
bool, classSBoolis a singleton subclass ofint. Unlikebool, it can be further subclassed.SBooland its subtypes can also do (non-shortcut) Boolean logic using Python bitwise operators.Boolean operation
symbol
dunder
not
~__invert__and
&__and__or
|__or__xor
^__xor__While compatible with Python short-cut logic, , the not operator unfortunately always returns a bool.
Tip
Use the bitwise ~ operator to return an opposite SBool instance or subclass instance.
Note
These operators are contravariant, that is they will return the instance of the latest common ancestor of their arguments. More specifically, the instance returned will have the type of the least upper bound in the inheritance graph of the classes of the two arguments.
Warning
The “bitwise” operators can raise TypeError exceptions when applied against an SBool and objects not descended from int.
- static __new__(cls, witness: object) Self¶
- static __new__(cls, witness: object, flavor: Hashable | NoValue = no_value) Self
new
- param witness:
Determines truthiness of the
SBool.- param flavor:
Ignored, here for Liskov substitution principle
- returns:
The truthy or falsy
SBoolclass instance.
- __init__(witness: object) None¶
- __init__(witness: object, flavor: Hashable) None
init
- param witness:
Determines the truthiness of the
SBool.- param flavor:
Ignored by
SBool, here only to support the Liskov Substitution Principle.
- __invert__() int¶
bitwise invert operator
Inverts the value of an SBool.
- returns:
The inverted value of the SBool.
- __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.
- __repr__() str¶
repr string
A string to reproduce the
SBool.- returns:
‘SBool(True)’ if truthy, ‘SBool(False)’ if falsy.
- __str__() str¶
user string
A string meaningful to an end user.
- returns:
‘TRUTH’ if truthy, ‘LIE’ if falsy.