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.

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_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 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 pickle and copy how to reconstruct the T_Bool singleton by calling T_Bool() rather than rebuilding the object directly.

  • reconstruction goes through __new__, so the flavor cache is consulted

  • unpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter

  • copy and deepcopy use the same protocol, so they return the singleton unchanged

returns:

The pair (TBool, (None,)), which pickle uses to rebuild the singleton. The None argument is ignored and is there only to satisfy the Liskov substitution principle.

Important

Without this, pickle would construct a duplicate T_Bool outside the instance dict, and is comparisons against it would silently fail.

__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 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 pickle and copy how to reconstruct the F_Bool singleton by calling F_Bool() rather than rebuilding the object directly.

  • reconstruction goes through __new__, so the flavor cache is consulted

  • unpickling returns the existing singleton of that flavor, or creates and caches it in a fresh interpreter

  • copy and deepcopy use the same protocol, so they return the singleton unchanged

returns:

The pair (FBool, (None,)), which pickle calls to rebuild the sentinel. The argument is ignored and is there only to satisfy the Liskov substitution principle.

Important

Without this, pickle would construct a duplicate F_Bool outside the instance dict, and is comparisons against it would silently fail.

__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 instance.

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

NEVER

var NEVER:

The falsy singleton TF_Bool instance.