Python 3.4+enumterm
@unique
checking
The @unique decorator forbids duplicate values in an enumeration — it raises ValueError when there are any.
Syntax
from enum import unique
@unique
class X(Enum): ...
Without the decorator a repeated value is not an error at all: the second name silently becomes an alias of the first member, both names point at one object, iteration shows only the first, and __members__ holds both. Reach for @unique where a duplicate almost certainly means a typo; if the aliases are deliberate (an old and a new name for the same state), the decorator has to go.
Examples
from enum import Enum, unique
@unique
class Status(Enum):
ACTIVE = 1; INACTIVE = 2
# @unique
# class Bad(Enum):
# A = 1; B = 1 # → ValueError!
Status.ACTIVE.value # → 1
Status.INACTIVE.value # → 2
See also
Open in the full glossarydocs.python.orgReport an inaccuracy