Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I subclass a subclass of Enum?

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_

like image 943
Toothpick Anemone Avatar asked Nov 04 '25 22:11

Toothpick Anemone


1 Answers

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)...

like image 99
Ethan Furman Avatar answered Nov 06 '25 13:11

Ethan Furman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!