Python 3 枚举类型:一个枚举类型继承另一个枚举类型不起作用?

3

我只是试图按照官方Python文档https://docs.python.org/zh-cn/3.9/library/enum.html中的8.13.13.2和8.13.13.4示例来创建Python 3中的枚举。

我的目标是创建一个可以进行迭代、比较并且拥有三个单独属性的枚举。但我一直找到这个错误:

AttributeError: can't set attribute

看起来是在__init__()构造函数中出现了错误。

代码:

我首先尝试了只有一个类的情况,就像这样:

class Hand(Enum):
    FIVE_OF_KIND = (6,'FIVE_OF_KIND',[5])
    FOUR_OF_KIND = (5,'FOUR_OF_KIND',[4,1])
    FULL_HOUSE = (4,'FULL_HOUSE',[3,2])
    THREE_OF_KIND = (3,'THREE_OF_KIND',[3,1,1])
    DOUBLE_PAIR = (2,'DOUBLE_PAIR',[2,2,1])
    PAIR = (1,'PAIR',[2,1,1,1])
    NOTHING = (0,'NOTHING',[1,1,1,1,1])

    def __init__(self, val, name, struct):
        self.val = val
        self.name = name
        self.struct = struct

    def __ge__(self, other):
        if self.__class__ is other.__class__:
            return self.value >= other.value
        return NotImplemented

    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.value > other.value
        return NotImplemented

    def __le__(self, other):
        if self.__class__ is other.__class__:
            return self.value <= other.value
        return NotImplemented

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value < other.value
        return NotImplemented

其次,使用如下两个类:
class OrderedEnum(Enum):
    def __ge__(self, other):
        if self.__class__ is other.__class__:
            return self.value >= other.value
        return NotImplemented

    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.value > other.value
        return NotImplemented

    def __le__(self, other):
        if self.__class__ is other.__class__:
            return self.value <= other.value
        return NotImplemented

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value < other.value
        return NotImplemented


class Hand(OrderedEnum):
    FIVE_OF_KIND = (6,'FIVE_OF_KIND',[5])
    FOUR_OF_KIND = (5,'FOUR_OF_KIND',[4,1])
    FULL_HOUSE = (4,'FULL_HOUSE',[3,2])
    THREE_OF_KIND = (3,'THREE_OF_KIND',[3,1,1])
    DOUBLE_PAIR = (2,'DOUBLE_PAIR',[2,2,1])
    PAIR = (1,'PAIR',[2,1,1,1])
    NOTHING = (0,'NOTHING',[1,1,1,1,1])

    def __init__(self, val, name, struct):
        self.val = val
        self.name = name
        self.struct = struct
1个回答

4

Enum对象已经有一个name属性(例如,请参见8.13.13.3),显然你不能设置它 - 当你考虑枚举应该如何行为时,这是有意义的。你可以通过以下方式实现你想要的效果:

from enum import Enum

class OrderedEnum(Enum):
    # Same as your code.

class Hand(OrderedEnum):

    FIVE_OF_KIND  = (6, [5])
    FOUR_OF_KIND  = (5, [4,1])
    FULL_HOUSE    = (4, [3,2])
    THREE_OF_KIND = (3, [3,1,1])
    DOUBLE_PAIR   = (2, [2,2,1])
    PAIR          = (1, [2,1,1,1])
    NOTHING       = (0, [1,1,1,1,1])

    def __init__(self, val, struct):
        # No need to set self.name. It's already handled.
        self.val = val
        self.struct = struct

for h in Hand:
    print((h.name, h.val, h.struct))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接