Created
August 17, 2022 19:25
-
-
Save thorwhalen/3b3ac5f6ecb68d314b56175da022fea3 to your computer and use it in GitHub Desktop.
Options for a singleton "Empty" value with pickability and repr
This file contains 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
from tested import validate_codec | |
Empty1 = type('Empty', (), {})() | |
print("\nEmpty1 = type('Empty', (), {})()") | |
print(f"{validate_codec(Empty1)=}") | |
_Empty2 = type('Empty', (), {}) | |
_Empty2.__module__ = __name__ | |
Empty2 = _Empty2() | |
print("\nsame, with _Empty2.__module__ = __name__; Empty2 = _Empty()") | |
print(f"{validate_codec(Empty2)=}") | |
_Empty3 = type('Empty', (), {}) | |
Empty3 = _Empty3() | |
Empty3.__module__ = __name__ | |
print("\nwith Empty3.__module__ = __name__ (added to instance instead of class)") | |
print(f"{validate_codec(Empty3)=}") | |
class _Empty4: ... | |
Empty4 = _Empty4() | |
print("\nDefining as ``class _Empty4: ...``") | |
print(f"{validate_codec(Empty4)=}") | |
class Empty5: ... | |
Empty5 = Empty5() # overridding class (now no one else can use it!" | |
print("\nOverwriding class Empty5 with instance Empty5") | |
print(f"{validate_codec(Empty5)=}") | |
class _Empty6: | |
def __repr__(self): | |
return f"Empty" | |
Empty6 = _Empty6() | |
print("\nLike Empty4, but with nice repr)") | |
print(f"{validate_codec(Empty6)=}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment