Skip to main content

NilType

Codecov PyPI PyPI - Downloads Python Version

NilType is a Python package that provides a Nil singleton object to represent a null or missing value in situations where None is a valid data value and cannot be used to signify the absence of data. This is especially useful in data models or functions where None might be a meaningful value, and a distinct placeholder is needed to indicate 'no value' or 'missing data'.

Installation

Install niltype using pip:

pip3 install niltype

Introduction

In Python, None is often used to represent the absence of a value. However, there are cases where None is a valid and meaningful value within your data model. In such situations, you need a different sentinel value to represent 'no value' or 'missing data'. This is where NilType comes into play by providing a Nil object.

Usage

Importing Nil

First, import Nil from the niltype package:

from niltype import Nil

Checking for Nil

You can check if a variable is Nil using the is operator:

if x is Nil:
    # x is missing or undefined
    pass

This check will only be True if x is exactly Nil.

Example Usage

Here's an example of using Nil to provide a default value in a function:

from niltype import Nil

def get(dictionary, key, default=Nil):
    try:
        return dictionary[key]
    except KeyError:
        if default is not Nil:
            return default
        raise

# Example usages:
get({}, 'key')            # Raises KeyError because no default is provided
get({}, 'key', None)      # Returns None
get({}, 'key', 'default') # Returns 'default'

In this example, the get function behaves similarly to dict.get(), but it raises a KeyError if the key is not found and no default value is provided. By using Nil as the default default value, you can distinguish between when a default has been provided (None or any other value) and when it hasn't.

Nil and Truthiness

The Nil object evaluates to False in boolean contexts:

if not Nil:
    print("Nil is Falsey")  # This will print

Using Nil in Type Annotations

You can use the Nilable type to indicate that a variable can be either a specific type or Nil:

from niltype import Nilable

def process(value: Nilable[int]) -> None:
    if value is Nil:
        print("Value is missing")
    else:
        print(f"Value is {value}")

process(10)    # Output: Value is 10
process(Nil)   # Output: Value is missing

The Nilable type is a type alias that allows for a value of type _T or NilType, providing better type hints and checks when using type annotations.

When to Use Nil

Use Nil when you need a unique sentinel value to represent missing or undefined data, especially in the following scenarios:

  • Default Parameters: Distinguish between an explicit None and an unspecified parameter.
  • Data Models: When None is a valid value within your data model, and you need to represent the absence of a value.
  • Optional Values: Clearly indicate optional values without overloading None.

Metadata

Release files for niltype 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for niltype 1.0.2
File Size Uploaded
niltype-1.0.2.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for niltype 1.0.2
File Interpreter ABI Platform
niltype-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / niltype-1.0.2.tar.gz

Download URL niltype-1.0.2.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
82bf705a898552467a393a95f3c750d82cd7c9361705c79f4cf416aeaa50b817
BLAKE2b-256 checksum
How to use checksums
9c33ce45c90efcf7f951de4fb1e6032fcb5447b1796c951d86e33324fcff2491
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / niltype-1.0.2-py3-none-any.whl

Download URL niltype-1.0.2-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5a26b50bb212689e3812a5010e8294d16bd7a179dbd051418ee878bad549754
BLAKE2b-256 checksum
How to use checksums
41d2d3dee14b063ce2ce641a5f6d40b3537622373678c9db9ccbe67d15029b13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page