All Courses

How to use Enum in python?

By Albert, 2 years ago
  • Bookmark
0

How to create and use Enum data type in python?

Enum
Python
1 Answer
0

Install enum


pip install enum34


Creating an enum :


from enum import Enum

class Color(Enum):
    red = 1
    green = 2
    blue = 3

print(Color.red) # Color.red
print(Color(1)) # Color.red
print(Color['red']) # Color.red


Enum Iteration :


Enums are iterable:


class Color(Enum):
    red = 1
    green = 2
    blue = 3

[c for c in Color] # [<Color.red: 1>, <Color.green: 2>, <Color.blue: 3>]

Your Answer

Webinars

Why You Should Learn Data Science in 2023?

Jun 8th (7:00 PM) 289 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More