I recently came across this code:
@unique
class NetlistKind(IntEnum):
Unknown = 0
LatticeNetlist = 1
QuartusNetlist = 2
XSTNetlist = 4
CoreGenNetlist = 8
All = 15
What does the @unique
decorator do, and what is its purpose in the above snippet?
unique
is a class decorator for Enum
that raises a ValueError if there are any duplicate enumeration values.
This code
from enum import unique, Enum
@unique
class Mistake(Enum):
ONE = 1
TWO = 2
THREE = 3
FOUR = 3
Produces this error:
ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE
From the documentation:
By default, enumerations allow multiple names as aliases for the same value. When this behavior isn’t desired, [unique] can be used to ensure each value is used only once in the enumeration
[unique is] a class decorator specifically for enumerations. It ensures only one name is bound to any one value [in an enum]. [unique] searches an enumeration’s
__members__
gathering any aliases it finds; if any are found ValueError is raised with the details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With