Python Class inheriting Enum only vs. both Str, Enum

Viewed 24

Whats the difference between an enum class that inherits Enum compared with one that inherits Str, Enum, and when do you decide to use the one that has both Str, Enum?

I tried printing out the names and values of both types of enum instances, but they appear similar

from enum import Enum

class Animals(Enum):
    CAT = "cat"
    DOG = "dog"
print(Animals.CAT.name, type(Animals.CAT.name), Animals.CAT.value, type(Animals.CAT.value))

class Colors(str, Enum):
    BLUE = "blue"
    RED = "red"
print(Colors.RED.name, type(Colors.RED.name), Colors.RED.value, type(Colors.RED.value))

Output:

CAT <class 'str'> cat <class 'str'>
RED <class 'str'> red <class 'str'>
0 Answers
Related