【发布时间】: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