小编Ruv*_*aba的帖子

从多个值python中获取Enum名称

我试图获得枚举的名称,给出其多个值之一:

class DType(Enum):
    float32 = ["f", 8]
    double64 = ["d", 9]
Run Code Online (Sandbox Code Playgroud)

当我试图得到一个值给它的名字时它起作用:

print DType["float32"].value[1]  # prints 8
print DType["float32"].value[0]  # prints f
Run Code Online (Sandbox Code Playgroud)

但是当我尝试从给定值中获取名称时,只会出现错误:

print DataType(8).name
print DataType("f").name
Run Code Online (Sandbox Code Playgroud)

提高ValueError("%s不是有效的%s"%(值,cls.名称))

ValueError:8不是有效的DataType

ValueError:f不是有效的DataType

有没有办法做到这一点?或者我使用了错误的数据结构?

python enums multiple-value

9
推荐指数
1
解决办法
4362
查看次数

标签 统计

enums ×1

multiple-value ×1

python ×1