subtypable

class pythonic_fp.booleans.subtypable.SBool

Bases: int

Subtypable Boolean

Like Python’s built in bool, class SBool is a singleton subclass of int. Unlike bool, it can be further subclassed.

SBool and 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 = NoValue()) Self

new

param witness:

Determines truthiness of the SBool.

param flavor:

Ignored

returns:

The truthy or falsy SBool class 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 for support the Liskov Substitution Principle.

__invert__() int

~self

__and__(other: int) int

Return self&value.

__or__(other: int) int

Return self|value.

__xor__(other: int) int

Return self^value.

__repr__() str

repr string

  • ‘SBool(True)’ if truthy

  • ‘SBool(False)’ if falsy

returns:

A string to reproduce the SBool.

__str__() str

user string

  • ‘TRUTH’ if truthy

  • ‘LIE’ if falsy

returns:

A string meaningful to an end user.

pythonic_fp.booleans.subtypable.TRUTH: Final[SBool] = SBool(True)

TRUTH

var TRUTH:

The truthy singleton of type SBool.

pythonic_fp.booleans.subtypable.LIE: Final[SBool] = SBool(False)

LIE

var LIE:

The falsy singleton of type SBool.