将字符串转换为Enum子类的对应实例的正确方法是什么?似乎getattr(YourEnumType, str)做的工作,但我不确定它是否足够安全。

举个例子,假设我有一个枚举

class BuildType(Enum):
    debug = 200
    release = 400

给定字符串'调试',我怎么能得到BuildType.debug作为结果?


当前回答

对@rogueleaderr的回答进行了改进:

class QuestionType(enum.Enum):
    MULTI_SELECT = "multi"
    SINGLE_SELECT = "single"

    @classmethod
    def from_str(cls, label):
        if label in ('single', 'singleSelect'):
            return cls.SINGLE_SELECT
        elif label in ('multi', 'multiSelect'):
            return cls.MULTI_SELECT
        else:
            raise NotImplementedError

其他回答

我只是想通知这在python 3.6中不起作用

class MyEnum(Enum):
    a = 'aaa'
    b = 123

print(MyEnum('aaa'), MyEnum(123))

你必须像这样以元组的形式给出数据

MyEnum(('aaa',))

编辑: 事实证明这是错误的。感谢一位指出我错误的评论者

由于MyEnum['dontexist']将导致错误KeyError: 'dontexist',您可能喜欢无声地失败(例如。返回None)。在这种情况下,你可以使用下面的静态方法:

class Statuses(enum.Enum):
    Unassigned = 1
    Assigned = 2

    @staticmethod
    def from_str(text):
        statuses = [status for status in dir(
            Statuses) if not status.startswith('_')]
        if text in statuses:
            return getattr(Statuses, text)
        return None


Statuses.from_str('Unassigned')

另一个选择(特别有用,如果你的字符串不映射1-1到你的enum情况)是添加一个static方法到你的enum,例如:

class QuestionType(enum.Enum):
    MULTI_SELECT = "multi"
    SINGLE_SELECT = "single"

    @staticmethod
    def from_str(label):
        if label in ('single', 'singleSelect'):
            return QuestionType.SINGLE_SELECT
        elif label in ('multi', 'multiSelect'):
            return QuestionType.MULTI_SELECT
        else:
            raise NotImplementedError

然后你可以输入question_type = questiontype。from_str('singleSelect')

对@rogueleaderr的回答进行了改进:

class QuestionType(enum.Enum):
    MULTI_SELECT = "multi"
    SINGLE_SELECT = "single"

    @classmethod
    def from_str(cls, label):
        if label in ('single', 'singleSelect'):
            return cls.SINGLE_SELECT
        elif label in ('multi', 'multiSelect'):
            return cls.MULTI_SELECT
        else:
            raise NotImplementedError

这个功能已经内置在Enum中:

>>> from enum import Enum
>>> class Build(Enum):
...   debug = 200
...   build = 400
... 
>>> Build['debug']
<Build.debug: 200>

成员名是大小写敏感的,所以如果用户输入被转换,你需要确保大小写匹配:

an_enum = input('Which type of build?')
build_type = Build[an_enum.lower()]