I know, I know, there are already similar questions in here and there. But their questions and answers are not exactly what I am looking for. Besides, they are locked question so I can not add a new answer to them. SMH.
Firstly, let's clarify the question to understand its scope. When using enum in other static languages like this:
public enum Size
{
SMALL=0,
MIDIUM=1,
LARGE=2,
BIG=2 // There can possibly be an alias
}
we want it to help us in:
- Guard against typo when referencing a value. For example,
var foo = Size.SMALL
is valid,var bar = Size.SMAL
should generate a lousy error. - Enum values can support strings, Such as
HTTP404 = "Not Found", HTTP200 = "OK", ...
. (Therefore those implementations based onrange(N)
is unacceptable.) - When defining a parameter as a specific Enum type, it serves as a regulation to accept only that kind of values. For example,
public void Foo(Size size) {...}
I also want the values to be first-class citizen in my Enum solution. Meaning, my functions
def parser(value_from_the_wire): ...
would like to consume some native values (such as an integer or a string etc.), rather than to consume an Enum member. This is the tricky part in standard Enum in Python 3:assert 2 == MY_ENUM.MY_VALUE
would only work when MY_ENUM was derived fromIntEnum
(and there is no defaultStrEnum
although it is not difficult to subclass one by yourself)assert 2 in MY_ENUM
wouldn't work, even if MY_ENUM was derived fromIntEnum
.