0

我想在 python 中创建一个 Enum 类。我还需要一些 get_str() 方法,例如:

class Operation (object):
    START = 0
    STOP = 1
    (...)

    def get_str(self):
        operation_dispatcher = {
             Operation.START: "start", 
             Operation.STOP: "stop",
             (...)

             }
    return operation_dispatcher[self]

但不幸的是,这种方法行不通。对象是整数,我收到“int”对象没有属性“get_str”的错误消息......你知道如何实现该功能吗?

我试图做类似的事情:

Operation.get_str(operation_reference)以及operation_reference.get_str()

更新:

class EnumMeta(type):
    def __getattribute__(self, name):
        return self(super(EnumMeta, self).__getattribute__(name))

class Enum(object):
    __metaclass__ = EnumMeta

    def __init__(self, value):
        super(Enum, self).__init__()

        self.value = value[0]
        self.repr = value[1]

    def __eq__(self, other):
        if isinstance(other, Enum):
            return self.value == other.value
        elif isinstance(other, int):
            return self.value == other
        else:
            return object.__eq__(Enum, other)

    def __repr__(self):
        return str(self.repr)

class Operation(Enum):
    START = (0, "start")
    STOP = (1, "stop")
    (...)

operation_dispatcher = {
             Operation.START: start_method, 
             Operation.STOP: stop_method,
             (...) }

# invoking
operation_dispatcher[Operation.START.value]()
4

2 回答 2

0

我建议使用元类来实现您的目标,以最小化客户端代码。所以首先检查下面的元类:

class EnumMeta(type):
    def __getattribute__(self, name):
        actual_value = super(EnumMeta, self).__getattribute__(name)
        if isinstance(actual_value, self):
            return actual_value
        else:
            new_value = self(actual_value)
            super(EnumMeta, self).__setattr__(name, new_value)
            return new_value

它只是使用属性值作为构造函数参数覆盖__getattribute__并返回子类的实例。它还会更新原始值,以免每次都创建新实例,并且使用对象的引用进行相等性检查

然后像这样定义一个Enum类:

class Enum(object):
    __metaclass__ = EnumMeta

    def __init__(self, value):
        super(Enum, self).__init__()

        self.value = value[0]
        self.repr = value[1]

    def __repr__(self):
        return str(self.repr)

这个基类实现了 equals ( ==) 运算符,使用 int 值和__repr__方法进行比较,以返回枚举的字符串表示形式。所以你去:

class Operation(Enum):
    START = (0, "start")
    STOP = (1, "stop")

>>> Operation.START == Operation.START
True
>>> Operation.START is Operation.START
True
>>> Operation.START == Operation.STOP
False
>>> Operation.START
"start"
>>> repr(Operation.STOP)
"stop"
于 2015-08-03T07:52:34.223 回答
0

EnumPython 中的 s 是:

  • 从 Python 3.4 开始内置
  • 可作为Python 3.3 到 Python 2.4的向后移植
  • 在一个增强的库中可用,该库还包括一个基于类NamedTuple和一个Constant

使用它,您的代码将如下所示:

from aenum import IntEnum   # or from enum import IntEnum

class Operation(IntEnum):
    START = 0
    STOP = 1

>>> Operation.START
<Operation.START: 0>

>>> Operation['START']
<Operation.START: 0>

>>> Operation(0)
<Operation.START: 0>

>>> Operation.STOP is Operation.STOP
True

>>> list(Operation)
[<Operation.START: 0>, <Operation.STOP: 1>]

>>> Operation.STOP.name
'STOP'

>>> Operation.STOP.value
1
于 2015-08-03T17:04:23.020 回答