Skip to content

Instantly share code, notes, and snippets.

@SeanPesce
Last active March 18, 2022 16:02
Show Gist options
  • Save SeanPesce/535150661c282c95a06db98c3c7e5640 to your computer and use it in GitHub Desktop.
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
#!/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