An Enum member is not its value¶
Level: 201 · for Python programmers
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
One line: Color.RED is an object, Color.RED == 1 is False unless the class is an IntEnum, Color(1) looks a member up by value and Color['RED'] by name, two members with one value are one member and an alias, and iteration yields each member once, in definition order.
What the finished page has to answer¶
- Members are singletons, so
isis the honest comparison and==between members works because of it;.nameand.value;auto(). - Aliases: a second member with the same value is the first,
@uniqueto forbid it, and what iteration andlen()do with an alias. IntEnumandStrEnum(3.11): comparable with plain values, sortable, and written byjson.dumpsas their value where a plainEnummember raisesTypeError, which isjsonis not Python section 2.Flagand the bitwise operators, and what a combination that is not a named member prints as.matchon an enum:case Color.RED:is a value pattern, andcase RED:binds a new name to anything, which is the one place the dotted spelling is load-bearing.- Members cannot be instantiated or subclassed once the enum has members, and why.
See also¶
jsonis not Python: a plain member has no spellingisis not==: why members compare by identityenum↗ in the library reference and the enum HOWTO ↗- What an enum is ↗ in the Rust library, where a variant is a type of its own and the compiler checks the
match