OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Best way to create Enums with hierarchy

  • Thread starter Thread starter user12138762
  • Start date Start date
U

user12138762

Guest
I want to create Enums that have hierarchy to simplify argument validation, type checks, etc. For example, say I want to enumerate the different types of employees in a company, split across department:

Code:
class ProductEmployee(Enum):
    PM = 1
    ANALYST = 2
    MANAGER = 3

class EngEmployee(Enum):
    DATA_ENG = 1
    FRONTEND_DEV = 2
    BACKEND_DEV = 3

What I tried: Create a new Enum encompassing both:

Code:
class Employee(Enum):
    PM = ProductEmployee.PM
    ANALYST = ProductEmployee.ANALYST
    MANAGER = ProductEmployee.MANAGER
    DATA_ENG = EngEmployee.DATA_ENG
    FRONTEND_DEV = EngEmployee.FRONTEND_DEV
    BACKEND_DEV = EngEmployee.BACKEND_DEV

Expected behavior is that different types can both be considered the same "supertype." For example, an integer is both an int and a numbers.Number, and a float is likewise a numbers.Number:

Code:
print(isinstance(1, numbers.Number))                    # True
print(isinstance(1, int))                               # True
print(isinstance(1.2, numbers.Number))                  # True

So ideally, I would want an Employee.PM to count as both an Employee and as a ProductEmployee. Here's what I actually get:

Code:
print(isinstance(Employee.PM, Employee))                # True
print(isinstance(Employee.PM, ProductEmployee))         # False
print(isinstance(Employee.PM._value_, ProductEmployee)) # True

Technically this can work, but the fact that I have to use ._value_ feel wrong. Is there a better way to create type hierarchies using Enum?
<p>I want to create Enums that have hierarchy to simplify argument validation, type checks, etc. For example, say I want to enumerate the different types of employees in a company, split across department:</p>
<pre><code>class ProductEmployee(Enum):
PM = 1
ANALYST = 2
MANAGER = 3

class EngEmployee(Enum):
DATA_ENG = 1
FRONTEND_DEV = 2
BACKEND_DEV = 3
</code></pre>
<p>What I tried: Create a new Enum encompassing both:</p>
<pre><code>class Employee(Enum):
PM = ProductEmployee.PM
ANALYST = ProductEmployee.ANALYST
MANAGER = ProductEmployee.MANAGER
DATA_ENG = EngEmployee.DATA_ENG
FRONTEND_DEV = EngEmployee.FRONTEND_DEV
BACKEND_DEV = EngEmployee.BACKEND_DEV
</code></pre>
<p>Expected behavior is that different types can both be considered the same "supertype." For example, an integer is both an int and a numbers.Number, and a float is likewise a numbers.Number:</p>
<pre><code>print(isinstance(1, numbers.Number)) # True
print(isinstance(1, int)) # True
print(isinstance(1.2, numbers.Number)) # True
</code></pre>
<p>So ideally, I would want an <code>Employee.PM</code> to count as both an <code>Employee</code> and as a <code>ProductEmployee</code>. Here's what I actually get:</p>
<pre><code>print(isinstance(Employee.PM, Employee)) # True
print(isinstance(Employee.PM, ProductEmployee)) # False
print(isinstance(Employee.PM._value_, ProductEmployee)) # True
</code></pre>
<p>Technically this can work, but the fact that I have to use <code>._value_</code> feel wrong. Is there a better way to create type hierarchies using Enum?</p>
 
Top