@unique decorator在python中做什么?

2024-09-30 20:27:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近从一个stackoverflow question中看到了这段代码:

@unique
class NetlistKind(IntEnum):
  Unknown = 0
  LatticeNetlist = 1
  QuartusNetlist = 2
  XSTNetlist = 4
  CoreGenNetlist = 8
  All = 15

def __contains__(self, item):
  return  (self.value & item.value) == item.value

@unique修饰符做什么,它在这个代码片段中的用途是什么?在


Tags: 代码selfvalueitemstackoverflowunknownclassunique
1条回答
网友
1楼 · 发布于 2024-09-30 20:27:43

来自documentation(谢谢@HFBrowning):

[@unique is] a class decorator specifically for enumerations. It searches an enumeration’s members gathering any aliases it finds; if any are found ValueError is raised with the details

基本上,如果存在任何重复的枚举值,则会引发错误。在

这个代码

 class Mistake(Enum):
     ONE = 1
     TWO = 2
     THREE = 3
     FOUR = 3

产生此错误:

^{pr2}$

相关问题 更多 >