How to use Enum in python?

By Albert, 2 months 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

How To Land a Job in Data Science

Jun 24th (7:00 PM) 238 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More