Skip to main content

Superclass for null types parallel to, but different from, None

Project description

PyPI Package latest release PyPI Package monthly downloads Supported versions Supported implementations

Helps define ‘null’ types different from, but parallel to, None.

None is a great sentinel value and a classic implementation of the null object pattern. But there are times that you need more than one to represent different aspects of emptiness. “Nothing there” is logically different from “undefined,” “prohibited,” “end of data” and other kinds of null.

Its core function is representing emptiness and falsity in a way that doesn’t overload None (or False, 0, {}, [] or any of the other “there’s nothing here!”)

This helps create designated identifiers with specific meanings such as Passthrough, Prohibited, and Undefined.

Usage

from nulltype import NullType

Empty = NullType('Empty')

# following just to show it's working
assert bool(Empty) == False
assert len(Empty) == 0
assert list(Empty) == []
assert Empty.some_attribute is Empty
assert Empty[22] is Nothing
assert Empty("hey", 12) is Empty

That created a custom NullType. You can create as many of them as you like. For your convenience, two default values, Null and Nothing, are exported. That way, if you don’t really want to create your own, you can import a pre-constituted null value, such as:

from nulltype import Nothing

Dereferencing

Alternate null types can be particularly useful when parsing data or traversing data structures which might or might not be present. This is common in dealing with the data returned by REST APIs, for instance.

As one example, the documentation for Google’s Gmail API suggests the following code:

threads = gmail_service.users().threads().list(userId='me').execute()
if threads['threads']:
    for thread in threads['threads']:
        print 'Thread ID: %s' % (thread['id'])

But there is a lot going on there to avoid a problematic dereference. If instead you have a Nothing null type defined, the code is shorter (and avoids an extra, very transient variable):

results = gmail_service.users().threads().list(userId='me').execute()
for thread in results.get('threads', Nothing):
    print 'Thread ID: %s' % (thread['id'])

This model of “go ahead, get it if you can” works well for chains of access as well:

results.get("payload", Nothing).get("headers", Nothing)

Will return the correct object if it’s there, but Nothing otherwise. And if you then try to test it (e.g. with if or a logical expression) or iterate over it (e.g. with for), it will act as though it’s an empty list.

Nothing isn’t nothing. It’s something that will simplify your code.

Uniqueness

NullType instances are meant to be singletons, with just one per program. They almost all, though technically multiple NullType instances are reasonable, making it more of a multiton pattern.

The uniqueness of each singleton is currently not enforced, making it a usage convention rather than strict law. With even minimal care, this is a problem roughly 0% of the time.

Notes

Recent Changes

  • Version 2.0 starts major upgrade from just Boolean operations being nulled to essentially all sorts of accesses and updates being nulled. It defines two default NullType instances, Null and Nothing. The ability to have anonymous (unnamed) nulls has been removed as superfluous.

  • Automated multi-version testing managed with the wonderful pytest, pytest-cov, and tox. Successfully packaged for, and tested against, all late-model versions of Python: 2.6, 2.7, 3.2, 3.3, 3.4, 3.5a4, as well as PyPy 2.5.1 (based on 2.7.9) and PyPy3 2.4.0 (based on 3.2.5).

  • The author, Jonathan Eunice or @jeunice on Twitter welcomes your comments and suggestions.

Installation

pip install -U nulltype

To easy_install under a specific Python version (3.3 in this example):

python3.3 -m easy_install nulltype

(You may need to prefix these with “sudo “ to authorize installation. In environments without super-user privileges, you may want to use pip’s --user option, to install only for a single user, rather than system-wide.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

nulltype-2.0.4.zip (11.9 kB view details)

Uploaded Source

nulltype-2.0.4.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file nulltype-2.0.4.zip.

File metadata

  • Download URL: nulltype-2.0.4.zip
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nulltype-2.0.4.zip
Algorithm Hash digest
SHA256 13cd41a2c0d1289c403553da6d1a60c2c238423dcf5a14ded6cbcb37d72749dd
MD5 8e0205715be0aeb01996f45bf70199a0
BLAKE2b-256 ff67ede09d4551059e50d4c98c0953fb27bf6285b4de3253a4111905d8dbdcc6

See more details on using hashes here.

File details

Details for the file nulltype-2.0.4.tar.gz.

File metadata

  • Download URL: nulltype-2.0.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nulltype-2.0.4.tar.gz
Algorithm Hash digest
SHA256 0b3f5d216e2f5822f4e1cb2aceccbf97a4e3366374def214950949aa46289cda
MD5 12126750fbfbedf533c2b2a435cc4a12
BLAKE2b-256 795402c7d9a325b802776f43805e952c33222a7c2d7b56d4d73ef695bbc8bb27

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page