Post

PythonPython

d
divija reddy

Posted on 28th January 2023|14 views

0
votes

How Can I Represent An Enum In Python?

Could someone please tell me how can I represent an Enum in Python?

Answers
P
James ROBERT

Posted on 28th January 2023

Here is one of the way to represent an Enum in Python:

class Enum(set): 

def __getattr__(self, name): 

if name in self: 

return name raise AttributeError

 

Usage:

Animals = Enum(["elephant", "dog", "rat"]) 

print(Animals.elephant)

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos