我有一个枚举来表示函数的不同可能值。class A(Enum): NOT_FOUND = NONE INAQUATE = NONE POSITIVE = 1 # Some more但是,当从函数返回此枚举时,def search_function(target = 1): if target == 1: return A.INAQUATE else: return A.POSITIVE返回 ,而不是将程序分解为行。A.NOT_FOUNDA.INAQUATEPython 3.7.6>>> from enum import Enum>>> class A(Enum):... NOT_FOUND = None... INAQUATE = None... POSITIVE = 1... >>> def search_function(target = 1):... if target == 1:... return A.INAQUATE... return A.NOT_FOUND... >>> search_function()<A.NOT_FOUND: None>有没有办法正确返回?A.INAQUATE
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
当 s 具有重复值时,成员是完全相同的对象 - 只是由本身中的两个名称引用:EnumEnum
>>> list(A)
[<A.NOT_FOUND: None>, <A.POSITIVE: 1>]
>>> A.INAQUATE is A.NOT_FOUND
True
最简单的解决方案是为每个成员指定一个唯一的值。下一个最简单的解决方案是使用库1:aenum
from aenum import Enum, NoAlias
class A(Enum):
#
_settings_ = NoAlias
#
NOT_FOUND = None
INAQUATE = None
POSITIVE = 1
在使用中:
>>> list(A)
[<A.INAQUATE: None>, <A.NOT_FOUND: None>, <A.POSITIVE: 1>]
>>> A.INAQUATE is A.NOT_FOUND
False
铌使用枚举时,按值查找功能将丢失:NoAlias
>>> A(1)
Traceback (most recent call last):
...
TypeError: NoAlias enumerations cannot be looked up by value
添加回答
举报
0/150
提交
取消