Last active
March 24, 2021 07:16
-
-
Save linw1995/43396ece64a43684bd1db21e8d3b4d72 to your computer and use it in GitHub Desktop.
Enum class for creating enum members with name and value being same.
This file contains hidden or 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
| import enum | |
| class AutoValue(enum.Enum): | |
| # is same with the example of docs.python.org | |
| # https://docs.python.org/3/library/enum.html#using-automatic-values | |
| def _generate_next_value_(name, start, count, last_values): | |
| return name | |
| Color = AutoValue("Color", "red blue green") | |
| assert Color['red'] == Color.red | |
| assert Color('red') == Color.red |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment