Python Glossary Русский

Python 3.4+enumterm

enum.Enum.value

attributes

The value of an enum member — what stands right of = in the class definition.

Syntax

Color.RED.value  # 1
Color(1)         # Color.RED

The way back is calling the class with a value: Color(1) returns Color.RED, while an unknown value raises ValueError. A value can be any object, and auto() fills it in for you. IntEnum and StrEnum members already behave as numbers or strings, so .value is needed there less often.

Examples

from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
print(Color.GREEN.value)  # → 2
print(Color(1))  # → Color.RED
Color(7)  # → ValueError: 7 is not a valid Color

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy