Last active
February 17, 2016 03:46
-
-
Save 3noch/395997c0f5f622973020 to your computer and use it in GitHub Desktop.
Abstract, unambiguous nothingness in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.