Python – Best/Cleanest way to define constant lists or dictionarys

Put your constants into their own module:

# constants.py

RED = 1
BLUE = 2
GREEN = 3

Then import that module and use the constants:

import constants

print "RED is", constants.RED

The constants can be any value you like, I’ve shown integers here, but lists and dicts would work just the same.

Leave a Comment