Enum of enums in Python?

后端 未结 5 1249
萌比男神i
萌比男神i 2021-02-14 01:54

Is it possible to have an enum of enums in Python? For example, I\'d like to have

enumA
    enumB
        elementA
        elementB
    enumC
        elementC
          


        
5条回答
  •  遇见更好的自我
    2021-02-14 02:05

    I made an enum of enum implementing de __ getattr __ in the base enum like this

    def __getattr__(self, item):
        if item != '_value_':
            return getattr(self.value, item).value
        raise AttributeError
    

    In my case I have an enum of enum of enum

    class enumBase(Enum):
        class innerEnum(Enum):
            class innerInnerEnum(Enum):
               A
    

    And

    enumBase.innerEnum.innerInnerEnum.A
    

    works

提交回复
热议问题