Module singletons

Pythonic FP - Collection of singleton classes

final class pythonic_fp.fptools.singletons.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=Sentinel('Nada'))

Get an alternate value, defaults to Nada().

Parameters:

alt (Any)

Return type:

Any

class pythonic_fp.fptools.singletons.NoValue

Singleton class representing a missing value.

  • similar to None but

    • while None represents “returned no values”

    • NoValue() represents the absence of a value

  • Usage

    • import NoValue from pythonic-fp.fptools.singletons and then

      • either use NoValue() directly

      • or define _noValue: Final[NoValue] = NoValue() don’t export it

    • compare using is and is not

      • not == or !=

      • None means returned no values, so None == None makes sense

      • if one or both values are missing, then what is there to compare?

final class pythonic_fp.fptools.singletons.Sentinel(sentinel_name)

Singleton classes representing a sentinel values.

  • intended for library code, not to be exported/shared between modules

    • otherwise some of its intended typing guarantees may be lost

  • useful substitute for None as a hidden sentinel value

    • allows None to be stored in data structures

    • allows end users to choose to use None or () as sentinel values

    • always equals itself (unlike NoValue)

  • usage

    • import Sentinel and then either

      • define _my_sentinel: Final[Sentinel] = Sentinel('my_sentinel')

      • or use Sentinel('my_sentinel') directly

    • compare using either

      • is and is not or == and !=

      • the Sentinel() value always equals itself

      • and never equals anything else, especially other sentinel values

Parameters:

sentinel_name (str)