简体   繁体   English

测试Enum列表中的成员资格失败

[英]Testing membership in list of Enum fails

I'm trying to check if an Enum is in a list of Enums but it is not working as expected. 我正在尝试检查一个Enum是否在Enum列表中,但是没有按预期工作。

If I write the code in a single script and I run it, it works fine: 如果我在一个脚本中编写代码并运行它,则可以正常工作:

>>> from enum import Enum
>>> class X(Enum):
...   A = 'a'
...   B = 'b'
...   C = 'c'
...
>>> s = [X.A, X.B]
>>> x = X.C
>>> y = X.B
>>> x in s
False
>>> y in s
True

Yet, in my code, the test doesn't seem to work. 但是,在我的代码中,该测试似乎无效。 This is what I added to debug the behavior: 这是我添加的用于调试行为的内容:

print(type(node.action.kind), node.action.kind)
print(type(KINDS), type(KINDS[0]), KINDS)
if node.action.kind in KINDS:
    print('FIRST TEST')
elif node.action.kind.value in [i.value for i in KINDS]:
    print('SECOND TEST')
else:
    print('NOT IN KINDS')

I would expect it to print FIRST TEST when the condition matches yet the code prints SECOND TEST : 我希望当条件匹配时它会打印FIRST TEST ,但是代码会显示SECOND TEST

<enum 'ActionKind'> ActionKind.TALK
<class 'list'> <enum 'ActionKind'> [<ActionKind.CALL: 'api_call'>, <ActionKind.WAIT: 'wait'>, <ActionKind.HANGUP: 'hangup'>]
NOT IN KINDS

<enum 'ActionKind'> ActionKind.WAIT
<class 'list'> <enum 'ActionKind'> [<ActionKind.CALL: 'api_call'>, <ActionKind.WAIT: 'wait'>, <ActionKind.HANGUP: 'hangup'>]
SECOND TEST

I can't see any apparent difference other than the enum is defined elsewhere and the list of KINDS is populated from imported constants in another file. 除了在其他地方定义的枚举之外,我看不到任何明显的区别,并且KINDS的列表是从另一个文件中的导入常量填充的。 What is happening? 怎么了? Why can't I test membership in list of Enum with the in operator? 为什么不能使用in运算符测试Enum列表中的成员资格?

I found the cause of the problem. 我找到了问题的原因。 I still don't understand it but I will leave this here in case it helps anyone. 我仍然不了解它,但是如果有任何帮助,我将在此保留。 The problem was that the enum was being reloaded. 问题在于该枚举正在重新加载。 Once reloaded, even though the printout for print(type(node.action.kind).__module__ matched, the enums were not. 重新加载后,即使print(type(node.action.kind).__module__的打印输出匹配,枚举也不匹配。

The following code is the minimal code that reproduces the error: 以下代码是重现该错误的最少代码:

import mylib.classes
from mylib.classes import *
import mylib.renderer
from mylib.renderer import *

nodes = get_nodes() # from classes
check_nodes(nodes) # from renderer
from importlib import reload
reload(mylib.classes)
reload(mylib.renderer)
check_nodes(nodes) # from renderer

The first call to check_nodes works fine and FIRST TEST is printed. 第一次调用check_nodes可以正常工作,并打印FIRST TEST After reloading the modules it stops working and SECOND TEST is printed instead. 重新加载模块后,它将停止工作,并打印SECOND TEST This code is part of a script that runs in the background and reloads the code between iterations in order for changes to make effect. 该代码是脚本的一部分,该脚本在后台运行,并在每次迭代之间重新加载代码,以使更改生效。 We only use this functionality during development. 我们仅在开发期间使用此功能。

If it helps, I am on Python 3.7.0 (default, Oct 2 2018, 09:19:48) [Clang 9.0.0 (clang-900.0.39.2)] 如果有帮助,我使用的是Python 3.7.0(默认,2018年10月2日,09:19:48)[Clang 9.0.0(clang-900.0.39.2)]

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

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