Skip to content

Instantly share code, notes, and snippets.

@blakeNaccarato
Last active January 12, 2024 05:05
Show Gist options
  • Save blakeNaccarato/29ec09e3ee8d95e4d98bbbf97f708b7b to your computer and use it in GitHub Desktop.
Save blakeNaccarato/29ec09e3ee8d95e4d98bbbf97f708b7b to your computer and use it in GitHub Desktop.
Different kinds of Python enums

Enums

Different kinds of Python enums.

"""Makes the Enum return its keys as its values."""
from enum import Enum, auto, unique
@unique
class C(Enum):
@staticmethod
def _generate_next_value_(name, *_):
return name
T1 = auto()
T2 = auto()
T3 = auto()
print(C.T1)
"""This does both."""
from enum import Enum, auto, unique
@unique
class C(Enum):
@staticmethod
def _generate_next_value_(name, *_):
return name
T1 = auto()
T2 = auto()
T3 = auto()
def __get__(self, *_):
return self.name
print(C.T1)
"""Makes the Enum return the name of the key when called, eg: `C.T1` yields `"T1`."""
from enum import Enum, auto, unique
@unique
class C(Enum):
T1 = auto()
T2 = auto()
T3 = auto()
def __get__(self, *_):
return self.name
print(C.T1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment