Skip to content

Instantly share code, notes, and snippets.

@3noch
Last active February 17, 2016 03:46
Show Gist options
  • Save 3noch/395997c0f5f622973020 to your computer and use it in GitHub Desktop.
Save 3noch/395997c0f5f622973020 to your computer and use it in GitHub Desktop.
Abstract, unambiguous nothingness in Python
class Void(object):
"""A placeholder object which never equals itself and always evaluates to False in
conditions.
This is useful when you want an abstract, unambiguous way to delineate a value as
being "not-set".
Ideally, you would subclass this in your context to create a "personal" void class
no one would ever use besides you. For example:
class __MyVoid(Void):
pass
"""
__eq__ = lambda self, _: False
__ne__ = lambda self, _: True
__nonzero__ = lambda self: False
__repr__ = lambda self: '<Void>'
@3noch
Copy link
Author

3noch commented Feb 17, 2016

Ironically, Python does not have a truly abstract, unambiguous type to communicate "nothingness" or "value-not-set." Oft-times Python coders will use ad-hoc sentinel values/types to communicate "not-set." Examples include None or (). However, these sentinels are not abstract: they cannot be used in abstract code because they could feasibly be candidate values in an implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment