singletons.nada

Pythonic FP - Collection of singleton classes

final class pythonic_fp.singletons.nada.Nada

Singleton class representing & propagating failure.

  • singleton _nada: nada = Nada() represents a non-existent value

  • returns itself for arbitrary method calls

  • returns itself if called as a Callable with arbitrary arguments

  • interpreted as an empty container by standard Python functions

  • warning: non-standard equality semantics

    • comparison compares true only when 2 non-missing values compare true

    • thus a == b means two non-missing values compare as equal

Usage:

  • import Nada and then

    • either use Nada() directly

    • or define _nada: Final[Nada] = Nada() don’t export it

  • start propagating failure by setting a propagating value to Nada()

    • works best when working with expression

    • failure may fail to propagate

      • for a function/method with just side effects

      • engineer Nada() to fail to trigger side effects

  • test for failure by comparing a result to Nada() itself using

    • is and is not

  • propagate failure through a calculation using

    • == and !=

    • the Nada() value never equals itself

    • and never equals anything else

nada_get(alt: Any = Sentinel('Nada')) Any

Get an alternate value, defaults to Nada().