Enum of enums in Python?

后端 未结 5 2058
不思量自难忘°
不思量自难忘° 2021-02-14 01:20

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

    Solution based on attrs. This also allows to implement attributes validators and other goodies of attrs:

    import enum
    
    import attr
    
    
    class CoilsTypes(enum.Enum):
        heating: str = "heating"
    
    
    class FansTypes(enum.Enum):
        plug: str = "plug"
    
    
    class HrsTypes(enum.Enum):
        plate: str = "plate"
        rotory_wheel: str = "rotory wheel"
    
    
    class FiltersTypes(enum.Enum):
        bag: str = "bag"
        pleated: str = "pleated"
    
    
    @attr.dataclass(frozen=True)
    class ComponentTypes:
        coils: CoilsTypes = CoilsTypes
        fans: FansTypes = FansTypes
        hrs: HrsTypes = HrsTypes
        filter: FiltersTypes = FiltersTypes
    
    
    cmp = ComponentTypes()
    res = cmp.hrs.plate
    

提交回复
热议问题