close

How to extend Python Enum?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to extend Python Enum in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to extend Python Enum?

  1. How to extend Python Enum?

    While uncommon, it is sometimes useful to create an enum from many modules. The aenum1 library supports this with an extend_enum function:

  2. extend Python Enum

    While uncommon, it is sometimes useful to create an enum from many modules. The aenum1 library supports this with an extend_enum function:

Method 1

Subclassing an enumeration is allowed only if the enumeration does not define any members.

Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances.

So no, it’s not directly possible.

Method 2

While uncommon, it is sometimes useful to create an enum from many modules. The aenum1 library supports this with an extend_enum function:

from aenum import Enum, extend_enum

class Index(Enum):
    DeviceType    = 0x1000
    ErrorRegister = 0x1001

for name, value in (
        ('ControlWord', 0x6040),
        ('StatusWord', 0x6041),
        ('OperationMode', 0x6060),
        ):
    extend_enum(Index, name, value)

assert len(Index) == 5
assert list(Index) == [Index.DeviceType, Index.ErrorRegister, Index.ControlWord, Index.StatusWord, Index.OperationMode]
assert Index.DeviceType.value == 0x1000
assert Index.StatusWord.value == 0x6041

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read