【问题标题】:Enum not iterable in Python 2.7枚举在 Python 2.7 中不可迭代
【发布时间】:2015-04-21 10:56:08
【问题描述】:
from enum import Enum

class Shake(Enum):
    __order__ = 'vanilla chocolate cookies mint' # only needed in 2.x
    vanilla = 7
    chocolate = 4
    cookies = 9
    mint = 3

for shake in Shake:
    print shake

运行此代码时出现错误

  for shake in Shake:
TypeError: 'type' object is not iterable

Python 2.7 中的Enum 不支持迭代吗?如果我们创建一个Enum 类型的对象,它就可以工作。

【问题讨论】:

  • 鉴于您在使用the documentation 中的示例时遇到问题,您确实应该将此问题作为开发人员的问题提出。不过,他们可能会想要更多信息 - 您正在使用什么版本(enum 和 Python),您是如何安装 enum 等的。您是否检查过您是否正在使用他们的库 (即不是其他的 enum.py 会影响它 - 试试 print enum.__file__)?
  • enum 模块不包含在 Python 2.7 中。你安装了哪个enum 模块?
  • 我安装了 pip install Enum 导致问题,pip install enum34 解决了它

标签: python python-2.7 enums


【解决方案1】:

Python 3.4 Enum 类型的反向移植是enum34,而不是enum

【讨论】:

  • 哇,你是开发者! :)
猜你喜欢
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 2017-05-12
相关资源
最近更新 更多