Sunday, 12 September 2021

Python: How inheritance work with enum?

One enum class can inherit from other enum class only if the parent enumeration does not define any members.

 

enumSubclass.py

from enum import Enum

class Test(Enum):
    def describe(self):
        return self.name, self.value
        
class Test1(Test):        
    A = 1
    B = 1
    C = 23

print(Test1.A.describe())

 

Output

('A', 1)

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment