Python 3 Enums:继承另一个Enum的Enum不起作用?

时间:2017-02-19 12:11:53

标签: python python-3.x enums

我只是通过参考官方Python文档https://docs.python.org/3.4/library/enum.html,特别是8.13.13.2和8.13.13.4示例来尝试在Python 3中创建一个Enum。

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

__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

其次是两个类:

    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "cars", joinColumns = @JoinColumn(name = "your_entity_id"))
    @Column(name = "car")
    private Cars car;

1 个答案:

答案 0 :(得分:3)

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