简体   繁体   English

python如何获取枚举的名称

[英]python how to get name of the enum

I have an enum like this我有一个这样的枚举

class testEnum(Enum):
   Code_1 = "successful response"
   Code_2 = "failure response"

Then I have a method that takes the enum key name Code_1 and enum key value successful response as inputs.然后我有一个方法,它将枚举键名称Code_1和枚举键值successful response作为输入。

If I send testEnum.Code_1 then that resolves to successful response and not Code_1 .如果我发送testEnum.Code_1则解析为successful response而不是Code_1

I checked some documentation online that suggests to use testEnum.Code_1.name but that throws an error saing that 'name' doesn't exist for the enum item.我在网上查看了一些建议使用testEnum.Code_1.name文档,但这会引发错误,指出枚举项不存在“名称”。

Does anyone know how to get the name of the enum key ?有谁知道如何获取枚举键的名称?

I suspect that what's happened is that you're using the outdated pip-installable library called enum .我怀疑发生的事情是您使用的是过时的名为enum 的pip-installable 库。 If you did, you'd get something like如果你这样做了,你会得到类似的东西

>>> from enum import Enum
>>> class testEnum(Enum):
...    Code_1 = "successful response"
...    Code_2 = "failure response"
... 
>>> testEnum.Code_1
'successful response'
>>> testEnum.Code_1.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'name'

whereas with the "real" enum (either enum in the standard library if you're using modern Python, or the enum34 backport if you're not), you'd see而使用“真正的”枚举(如果您使用的是现代 Python,则使用标准库中的enum ,或者如果您不使用,则使用enum34 backport ),您会看到

>>> from enum import Enum
>>> class testEnum(Enum):
...    Code_1 = "successful response"
...    Code_2 = "failure response"
... 
>>> testEnum.Code_1
<testEnum.Code_1: 'successful response'>
>>> testEnum.Code_1.name
'Code_1'

You can confirm this independently by typing help(enum) and seeing whether you see "NAME / enum / MODULE REFERENCE / https://docs.python.org/3.6/library/enum " (as you should) or simply "NAME / enum - Robust enumerated type support in Python" if you're using the older one.您可以通过键入help(enum)并查看您是否看到“NAME / enum / MODULE REFERENCE / https://docs.python.org/3.6/library/enum ”(如您所见)或只是“NAME / enum - Python 中强大的枚举类型支持”,如果您使用的是较旧的。

You can start your investigation with the __dict__ that comes with your object.您可以使用对象附带的__dict__开始调查。 Interesting reading is found with有趣的阅​​读是发现

print(testEnum.__dict__)

In that dict you will see a good start which you can test with the following:在该 dict 中,您将看到一个良好的开端,您可以使用以下内容进行测试:

print(testEnum._member_names_)

which, indeed, yields这确实会产生

['Code_1', 'Code_2']

dir(testEnum) will give you the dictionary keys. dir(testEnum) 会给你字典键。

eg例如

dir(testEnum)

returns:返回:

['Code_1', 'Code_2', ' class ', ' delattr ', ' dict ', ' dir ', ' doc ', ' eq ', ' format ', ' ge ', ' getattribute ', ' gt ', ' hash ', ' init ', ' init_subclass ', ' le ', ' lt ', ' module ', ' ne ', ' new ', ' reduce ', ' reduce_ex ', ' repr ', ' setattr ', ' sizeof ', ' str ', ' subclasshook ', ' weakref '] ['Code_1'、'Code_2'、' class '、' delattr '、' dict '、' dir '、' doc '、' eq '、' format '、' ge '、' getattribute '、' gt '、' hash '、' init '、' init_subclass '、' le '、' lt '、' module '、' ne '、' new '、' reduce '、' reduce_ex '、' repr '、' setattr '、' sizeof ' , ' str ', ' subclasshook ', ' weakref ']

在python3.9中可以使用:

testEnum.__members__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM