How to use Enum in python?

By Albert, 8 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

Live Masterclass on : "How Machine Learns in Machine Learning"

Dec 9th (6:00 PM) 228 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More