NullType Sentinels
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
- gotcha NullType instances are intended to be singletons (one per program) but their uniqueness is not strictly enforced. While this is rarely an issue in practice, users should be mindful that multiple instances with the same name could technically exist if not careful with usage patterns.
- gotcha The library was last released in 2018 and was explicitly tested up to Python 3.7 pre-release. While generally compatible with newer Python versions, it is not actively tested against Python 3.8+ versions. Users on recent Python versions should verify compatibility, especially concerning subtle changes in built-in behaviors or type hinting.
Install
-
pip install nulltype
Imports
- NullType
from nulltype import NullType
- Empty
from nulltype import Empty
- Null
from nulltype import Null
- Nothing
from nulltype import Nothing
Quickstart
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!")