python - 在 python 3 中遍历枚举时,并非所有元素都出现

标签 python python-3.x enums

在下面的代码中,我将 b 分配给 Enum 中的函数 f。然而,当我遍历该枚举时,b 没有出现,尽管我仍然可以通过 E.b 访问它。有谁知道这里发生了什么?这只是一个错误吗?我正在使用 python 3.5.1。

In [42]: from enum import Enum
In [43]: def f(): pass
In [44]: class E(Enum):
    ...:     a = 4
    ...:     b = f
    ...:     c = 5
    ...:
In [45]: list(E)
Out[45]: [<E.a: 4>, <E.c: 5>]
In [46]: E.b
Out[46]: <function __main__.f>

最佳答案

描述符不会成为枚举的成员。如果您提供枚举行为,这些方法与值本身位于同一命名空间中,因此这是枚举区分它们的唯一方法。

关于python - 在 python 3 中遍历枚举时,并非所有元素都出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41752300/

相关文章:

python - pytest: `foo is Enum.FOO` 在本地测试为 True,在 travis-ci 上测试为 False

c - C中的枚举指针参数

python aiohttp-sse 在 6 个请求后超时

python - 处理XML标签并提取对应的标签内容

python - 通过将列表索引视为python中的键,将dict中的所有列表转换为dict

python - 退出脚本后如何返回到 python shell?

python - 在正则表达式交替中匹配最长字符串的保证方法

python - Pandas 数据透视表 - 增量列名称

python - 索引错误 : index 10000 is out of bounds for axis 0 with size 10000

typescript - 在另一个文件中扩展导入的枚举并使用它