Truthy Falsy Booleans

Truthy and Falsy are not just different values, but different classes.

class pythonic_fp.booleans.truthy_falsy.TF_Bool

Bases: SBool

Truthy-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_Bool itself. 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, and not builtins.

For example:

  • ~ALWAYS is NEVER

  • not 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_Bool or F_Bool, of TF_Bool to 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_Bool

Truthy 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” if other is another TF_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” if other is another TF_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” if other is another TF_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_Bool

Falsy 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” if other is another SBool.

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” if other is another SBool.

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” if other is another SBool.

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.

pythonic_fp.booleans.truthy_falsy.ALWAYS: Final[TF_Bool] = T_Bool()

ALWAYS

var ALWAYS:

The truthy singleton TF_Bool subtyped instance.

pythonic_fp.booleans.truthy_falsy.NEVER: Final[TF_Bool] = F_Bool()

NEVER

var NEVER:

The falsy singleton TF_Bool subtyped instance.