Different kinds of Python enums.
Last active
January 12, 2024 05:05
-
-
Save blakeNaccarato/29ec09e3ee8d95e4d98bbbf97f708b7b to your computer and use it in GitHub Desktop.
Different kinds of Python enums
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
"""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 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
"""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) |
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
"""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