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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s