3

Consider the following code:

from  enum import Enum

class SubclassOfEnum(Enum):
    x = 5
print(SubclassOfEnum.x)

class SubSubclassOfEnum(SubclassOfEnum):
    y = 6
print(SubSubclassOfEnum.y)

We get an error, TypeError: Cannot extend enumerations,

from: Python36\lib\enum.py", line 436, in _get_mixins_

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42

1 Answers1

6

Because subclassing Enums with members is specifically disallowed.

For general use-cases for Enum check out When and where to use....

For extending Enums (adding members to existing Enums, not subclassing them)...

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Thanks. The "specifically disallowed" link is broken since 3.11. It seems to have moved here: https://docs.python.org/3/howto/enum.html#restricted-enum-subclassing – Jérôme May 03 '23 at 16:16