singletons.sbool

Class SBool - Subclassable Singleton Booleans

Python does not permit bool to be subclassed, but int can be subclassed. Under-the-hood a bool is just an int. The SBool class inherits from int and relies on the underlying truthiness and falsiness of 1 and 0.

The Truth(truth: str) and Lie(lie: str) subclass constructors produce singletons based on their input parameters. When using type hints, declare variables of these types as type SBool.

Best practices when used with these subclasses are:

  • use == or != for pure Boolean comparisons

  • use is or not is if the type of truth matters

  • only use SBool as a type, never as a constructor

  • when using Python shortcut logic remember

    • an instance of Truth is truthy

    • an instance of Lie is falsy

    • shortcut logic is lazy

      • the last truthy thing evaluated is returned

      • and is not converted to a bool

    • the not statement converts a SBool to an actual bool

class pythonic_fp.singletons.sbool.Lie(flavor: str = '')

Falsy singleton SBool subclass.

Note

When using type hints, declare variables SBool, not Lie.

class pythonic_fp.singletons.sbool.SBool

Subclassable Boolean hierarchy.

This class’s sub-types represent different “flavors” of “truth” where each flavor has one unique “truthy” and one unique “falsy” instance.

Note

Python does not permit bool to be subclassed, but int can be. Under-the-hood a bool is just an int. This class inherits from int and relies on the underlying truthiness and falsiness of 1 and 0.

Important

Only use SBool as a type, never as a constructor.

class pythonic_fp.singletons.sbool.Truth(flavor: str = 'DEFAULT_TRUTH')

Truthy singleton SBool subclass.

Note

When using type hints, declare variables SBool, not Truth.