Python 如何使用枚举键访问和打印类的成员?
我有一个类“CocoPart”,它的定义如下:Python 如何使用枚举键访问和打印类的成员?,python,python-3.x,enums,Python,Python 3.x,Enums,我有一个类“CocoPart”,它的定义如下: class CocoPart(Enum): Wrist = 4 LShoulder = 5 LElbow = 6 LWrist = 7 RHip = 8 RKnee = 9 RAnkle = 10 LHip = 11 LKnee = 12 LAnkle = 13 REye = 14 LEye = 15 REar = 16 LEar = 1
class CocoPart(Enum):
Wrist = 4
LShoulder = 5
LElbow = 6
LWrist = 7
RHip = 8
RKnee = 9
RAnkle = 10
LHip = 11
LKnee = 12
LAnkle = 13
REye = 14
LEye = 15
REar = 16
LEar = 17
Background = 18
这些成员被进一步操纵。在程序的后面,我需要使用它们的名称访问它们。我该怎么做?您可以通过多种方式访问成员
>>> list(CocoPart)
[<CocoPart.Wrist: 4>, ...]
>>> CocoPart.__members__.items()
odict_items([('Wrist', <CocoPart.Wrist: 4>), ...])
>>> for name, member in CocoPart.__members__.items():
... print(name, member.value)
...
Wrist 4
LShoulder 5
...
>列表(CocoPart)
[, ...]
>>>CocoPart.\uuuuu成员\uuuuuuu.items()
odict_项目([(‘手腕’,),…]))
>>>对于名称,CocoPart中的成员。\u成员\u.items():
... 打印(名称、成员.值)
...
手腕4
肩5
...
您可以找到更多详细信息。您有两个名称访问选项:
CocoPart.Wrist # when you know at programming time
enum_name = 'Wrist'
CocoPart[enum_name] # when the name is stored in a variable
谢谢你的回复,但我的问题进一步恶化了。所以,我现在发布另一个问题:0