首页 > 解决方案 > IronPython中是否有类似于ENUM的东西

问题描述

我正在为使用 IronPython(Python 脚本引擎版本 2.7.0.40)的技术程序(ANSYS ACT)编写自定义脚本

在使用标准 CPhython 时,我经常使用 Enum 模块来使选项更具可读性。像这样的东西:

from enum import Enum, auto

class StressType (Enum):    
    ElemMean = auto()
    NodeAveraged = auto()
    ElemNodeUnaveraged = auto()
    
def doSomething (stress, stresstype):    
   if not isinstance(stresstype, StressType):   # Type checking
        raise TypeError('Type Error')
   if stresstype is StressType.ElemMean:
        do something with stress ...    
   elif (stresstype is StressType.NodeAveraged):
       do something else with stress 
    ...

但是,在自定义环境中,此模块似乎不存在。我收到此错误消息:

Starting Python script engine version 2.7.0.40 for extension xxx.
No module named enum 

Ironpython 有类似枚举数据类型的东西吗?最好是本机数据类型,因此不必安装任何模块。

谢谢和欢呼。

标签: enumsironpython

解决方案


aenum1除了提供比 stdlib 枚举库更多的功能外,还适用于 Python 2.7 和3.3+

--

1披露:我是Python stdlibEnumenum34backportAdvanced Enumeration ( aenum) 库的作者。


推荐阅读