在 Python 2 中迭代枚举

Iterating Enums in Python 2

我正在使用 IronPython 2.7.9,并已使用 pip (ipy -X:Frames -m pip install --user enum) 安装了 enums。所以我预计 enums to work.

在某种程度上,他们这样做:

S:\>ipy
IronPython 2.7.9 (2.7.9.0) on .NET 4.0.30319.42000 (64-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import Enum
>>>
>>> class Shake(Enum):
...     vanilla = 7
...     chocolate = 4
...     cookies = 9
...     mint = 3
...
>>> Shake.vanilla
7

... 但当我尝试遍历它们的值时却没有:

>>> for shake in Shake:
...     print shake
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected Array[Type], got int

我是不是误会了什么? Python 枚举与 IronPython 不兼容吗?

作为替代方案,我愿意使用 .Net 的枚举,但是 the documentation is entirely rudimentary。谁能指出有关 IronPython 中枚举的综合文档?

enum 包与 API 不对应。您需要 Python 3 向后移植,enum34.