Post

PythonPython

d
divija reddy

Posted on 19th April 2024|41 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 19th April 2024

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