from enum import Enum
class XIntegerChoices(Enum):
def __new__(cls, value, label):
obj = object.__new__(cls)
obj._value_ = value
obj._label = label
return obj
def __eq__(self, other):
if isinstance(other, int):
return other == self.value
if isinstance(other, XIntegerChoices):
return other.value == self.value
return False
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
if isinstance(other, int):
return self.value < other
if isinstance(other, XIntegerChoices):
return self.value < other.value
raise TypeError
def __le__(self, other):
return self.__eq__(other) or self.__lt__(other)
def __gt__(self, other):
if isinstance(other, int):
return self.value > other
if isinstance(other, XIntegerChoices):
return self.value > other.value
raise TypeError
def __ge__(self, other):
return self.__eq__(other) or self.__gt__(other)
def __hash__(self):
return hash(self.value)
@property
def value(self):
return self._value_
@property
def label(self):
return self._label
@classmethod
def members(self):
return list(self)
class Action(XIntegerChoices):
CREATED = (1, "创建")
if __name__ == '__main__':
print(Action.CREATED == 2)
print(Action.CREATED.label)
print(Action.members())
# False
# 创建
# [<Action.CREATED: 1>]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.