简体   繁体   English

python中的奇怪枚举行为

[英]Strange enum behavior in python

I have the following enum(it is truncated to make the examples cleaner) 我有以下枚举(为了使示例更简洁,它被截断了)

class ObjectNames(enum.Enum):
    full = 'full'

And this one: 还有这个:

class ObjectPoses(enum.Enum):
    front = 'front'
    left = 'left'
    right = 'right'

This two enums are tied with the following relation: 这两个枚举具有以下关系:

class ObjectNamePoseMatcher:
    match = {
        ObjectNames.full: [ObjectPoses.front, ObjectPoses.right, ObjectPoses.left],
}

So it basically creates a relation between an ObjectName and a pose. 因此,它基本上在ObjectName和姿势之间创建关系。 This is the setup. 这是设置。 At some point my code fails with KeyError when trying to make ObjectNamePoseMatcher.match lookup. 在某些时候,尝试进行ObjectNamePoseMatcher.match查找时,我的代码因KeyError失败。 Specifically, it fails with this error message: 具体来说,它失败并显示以下错误消息:

KeyError: <ObjectNames.full: 'full'> It fails here: KeyError: <ObjectNames.full: 'full'>在这里失败:

ObjectNamePoseMatcher.match[self._object_name] , ObjectNamePoseMatcher.match[self._object_name]

however 然而

>>self._object_name
<ObjectNames.full: 'full'>

and

>>ObjectNamePoseMatcher.match {<ObjectNames.full: 'full'>: [<ObjectPoses.front: 'front'>, <ObjectPoses.right: 'right'>, <ObjectPoses.left: 'left'>]}

So the key is there. 所以关键就在那里。 This is strange so I executed the following snippet: 这很奇怪,所以我执行了以下代码段:

for k,v in ObjectNamePoseMatcher.match.items():
    print(k,k==self.object_name)
    print(hash(k)==hash(self.object_name))
    print(id(k)==id(self.object_name))
    print(id(type(k))==id(type(self.object_name)))

And this results in: ObjectNames.full False True False False So ObjectNames.full and self._object_name have the same hash, but different types, id and type ids. 结果是: ObjectNames.full False True False False因此, ObjectNames.fullself._object_name具有相同的哈希,但是类型,ID和类型ID不同。 But hash is the same. 但是哈希是一样的。

After this I went to the very first place where self._object_name field is set. 之后,我转到设置self._object_name字段的第一个地方。 Executed the same snippet again, got the same result. 再次执行相同的代码片段,得到相同的结果。 I executed this snippet after this line: 我在这一行之后执行了以下代码片段:

d.object_name = ObjectNames.full However if at this breakpoint I re-execute this line d.object_name = ObjectNames.full但是,如果在此断点处我重新执行此行

d.object_name = ObjectNames.full and then execute the aforementioned snippet I get the following output: d.object_name = ObjectNames.full ,然后执行上述代码段,我得到以下输出:

ObjectNames.full True True True True So now hash, ids and type ids match! ObjectNames.full True True True True是现在,哈希,ID和类型ID匹配! For no reson, I just re-executed the same line after stopping at breakpoint. 毫无疑问,我只是在断点处停止后重新执行了同一行。

What could possibly go wrong here? 这里可能出什么问题了?

Thanks 谢谢

I think you want IntEnum not Enum . 我认为您想要IntEnum而不是Enum IntEnum s compare by integer values and so by transitivity to other unrelated enumerations. IntEnum按整数值进行比较,因此按对其他无关枚举的可传递性进行比较。 You're clearly expecting instances of ObjectNames and ObjectPoses to be comparable and I don't think Enum s work like that. 您显然希望ObjectNamesObjectPoses实例具有可比性,但我认为Enum不会那样工作。

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

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