Python 按值获取枚举名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38716288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:20:23  来源:igfitidea点击:

Get enumeration name by value

pythonenumsenumeration

提问by Jiloc

Are there any standard methods to get Enumeration names by value?

有没有标准的方法可以得到 Enumeration names by value?

An example:

一个例子:

class Example(enum.Enum):
    one = 1
    two = 2

ex_variable = 1

Given ex_variable, can I obtain the string contained in Example.one.name?

鉴于ex_variable,我可以获得包含在 Example.one.name 中的字符串吗?

回答by Nils Werner

>>> Example(1).name
'one'

also see the Python docs.

请参阅 Python 文档

回答by AKS