NullType Sentinels

2.3.1 · maintenance · verified Sat Apr 11

nulltype is a Python library (current version 2.3.1) that provides custom null values and sentinels, distinct from Python's built-in `None`, `False`, or `True`. It helps represent different states of 'emptiness' or 'undefined' values, such as `Passthrough`, `Prohibited`, or `Undefined`, without overloading standard Python nullish objects. The library aims to simplify code by allowing robust handling of potentially missing data in a consistent way. The last release was in June 2018, indicating a maintenance phase with infrequent updates.

Warnings

Install

Imports

Quickstart

Demonstrates how to create a custom `NullType` and its behavior when subjected to common Python operations (boolean evaluation, length, iteration, attribute access, item access, and calls). It also shows the use of the pre-defined `Empty`, `Nothing`, and `Null` sentinels.

from nulltype import NullType, Empty, Nothing, Null

# Create a custom null type
Undefined = NullType('Undefined')

# Test common Pythonic behaviors
assert bool(Undefined) is False
assert len(Undefined) == 0
assert list(Undefined) == []

# Accessing attributes, items, or calling a NullType instance
# By default, for custom NullTypes, attribute/call access returns Empty, and item access returns Nothing.
# For predefined sentinels like Empty, these operations return themselves.
assert Undefined.some_attribute is Empty
assert Undefined[22] is Nothing
assert Undefined("hey", 12) is Empty

# Using pre-defined sentinels
assert Empty.any_attr is Empty
assert Nothing[0] is Nothing
assert Null() is Null

print("NullType examples ran successfully!")

view raw JSON →