Python Glossary Русский

Python 3.4+enumterm

enum.Enum.name

attributes

The name of an enum member as a string — what stands left of = in the class definition.

Syntax

Color.RED.name  # 'RED'
Color['RED']    # Color.RED

name is handy for display and serialisation, and the way back is lookup by name with square brackets: Color['RED']. The name cannot be reassigned: enum members are immutable. An alias (a second name for the same value) reports the name of the first, canonical member.

Examples

from enum import Enum
class Color(Enum):
    RED = 1
    CRIMSON = 1  # псевдоним RED
print(Color.RED.name)  # → RED
print(Color['RED'] is Color.RED)  # → True
print(Color.CRIMSON.name)  # → RED — у псевдонима имя канонического члена

See also

Open in the full glossarydocs.python.orgReport an inaccuracy