Enum of enums in Python?

后端 未结 5 2049
不思量自难忘°
不思量自难忘° 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:21

    You can use namedtuples to do something like this:

    >>> from collections import namedtuple
    >>> Foo = namedtuple('Foo', ['bar', 'barz'])
    >>> Bar = namedtuple('Bar', ['element_a', 'element_b'])
    >>> Barz = namedtuple('Barz', ['element_c', 'element_d'])
    >>> bar = Bar('a', 'b')
    >>> barz = Barz('c', 'd')
    >>> foo = Foo(bar, barz)
    >>> foo
    Foo(bar=Bar(element_a='a', element_b='b'), barz=Barz(element_c='c', element_d='d'))
    >>> foo.bar.element_a
    'a'
    >>> foo.barz.element_d
    'd'
    

    This is not a enum but, maybe solves your problem

提交回复
热议问题