Last active
March 18, 2022 16:02
-
-
Save SeanPesce/535150661c282c95a06db98c3c7e5640 to your computer and use it in GitHub Desktop.
Python 3 convenience class for checking if a value is a valid Enum value
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
#!/usr/bin/env python3 | |
from enum import EnumMeta, Enum | |
class EnumExMeta(EnumMeta): | |
def __contains__(self, val): | |
try: | |
self(val) | |
except ValueError: | |
return False | |
return True | |
class EnumEx(Enum, metaclass=EnumExMeta): | |
""" | |
Derive from this class instead of Enum to use the "in" operator | |
""" | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment