PyPI: pythonic-fp-booleans

PyPI project pythonic-fp-booleans part of the pythonic-fp namespace projects.

For a proposed version 1.1.0 release.

Subtypable Boolean like classes

While still compatible with Python shortcut logic, these classes can be non-shortcut logically composed with Python’s bitwise operators. These classes are implemented with the Singleton Pattern.

Covariant class hierarchy

digraph Booleans {
    bgcolor="deepskyblue";
    int -> bool;
    int -> SBool;
    SBool -> "FBool(h1)";
    SBool -> "FBool(h2)";
    SBool -> "FBool(h3)";
    SBool -> TF_Bool;
    TF_Bool -> T_Bool;
    TF_Bool -> F_Bool;
}

Contravariant non-shortcut “bitwise” operators

Boolean operation

symbol

dunder

not

~

__invert__

and

&

__and__

or

|

__or__

xor

^

__xor__

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

These “bitwise” operators could raise TypeError exceptions when applied against an SBool and objects not descended from SBool.

Classes

Class SBool

Base of the hierarchy.

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

Class FBool

For when you need to deal with different “flavors” of the truth.

Each “flavor” corresponds to a hashable value. Instances of FBool are invariant in their flavor. Best to think of the “flavor” as an index.

Class TF_Bool

Class TF_Bool consists of just two disjoint subclasses, each one a singleton.

  • class T_Bool is the always truthy TF_Bool subtype

  • class F_Bool is the always falsy TF_Bool subtype

Getting Started