How to use enum in Python

Python 2.x does not have an equivalent of the enum in C or C++. Python 3.x (3.4 and later) has a builtin enum type.

You can install the backported Python 3.4 enum for Python 2.x easily from here:

$ sudo pip install enum34

Usage of this enum is simple:

from enum import Enum

class Colors(Enum):
    Red   = 1
    Green = 2
    Blue  = 3

c = Colors.Green

Tried with: Python 2.7.6 and Ubuntu 14.04

One thought on “How to use enum in Python

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.