Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3枚举:继承另一个枚举的枚举不';不行?_Python_Python 3.x_Enums - Fatal编程技术网

Python3枚举:继承另一个枚举的枚举不';不行?

Python3枚举:继承另一个枚举的枚举不';不行?,python,python-3.x,enums,Python,Python 3.x,Enums,我只是想通过参考官方Python文档,特别是8.13.13.2和8.13.13.4示例,在Python3中创建一个枚举 我的目标是有一个枚举,我可以迭代,比较,还有三个独立的属性。但我一直发现这个错误: AttributeError: can't set attribute 这似乎是\uuu init\uu()构造函数中的一个错误 代码: 我首先尝试了一个这样的课程: class Hand(Enum): FIVE_OF_KIND = (6,'FIVE_OF_KIND',[5])

我只是想通过参考官方Python文档,特别是8.13.13.2和8.13.13.4示例,在Python3中创建一个枚举

我的目标是有一个枚举,我可以迭代,比较,还有三个独立的属性。但我一直发现这个错误:

AttributeError: can't set attribute
这似乎是
\uuu init\uu
()构造函数中的一个错误

代码:

我首先尝试了一个这样的课程:

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
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))
班手(枚举):
五个种类=(6,'五个种类',[5])
种类四=(5,'种类四',[4,1])
满屋=(4,'FULL_HOUSE',[3,2])
三种类型=(3,'三种类型',[3,1,1])
双_对=(2,'DOUBLE_对',[2,2,1])
PAIR=(1,'PAIR',[2,1,1,1])
NOTHING=(0,'NOTHING',[1,1,1,1])
定义初始化(self、val、name、struct):
self.val=val
self.name=名称
self.struct=struct
定义(自身、其他):
如果self.\uuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
返回self.value>=其他.value
返回未执行
定义(自身、其他):
如果self.\uuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
返回self.value>other.value
返回未执行
定义(自我、其他):
如果self.\uuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
返回self.value=other.value
返回未执行
定义(自身、其他):
如果self.\uuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
返回self.value>other.value
返回未执行
定义(自我、其他):
如果self.\uuuuuu class\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

return self.value
Enum
对象已经有一个
名称
属性(例如,请参见),显然不允许您对其进行设置–这在您考虑枚举的行为时是有意义的。您可以通过以下方式实现您想要的:

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
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))