Coloring weekdays with enums My aim is to assign a color to each workday in a week. So if a user enters a day as a str, then I can know which color it should have.

Because colors and workdays are fixed, I make them as enum.Enum instance.


Is enum.Enum a proper choice for colors and workdays?
To link day and color, I choose dictionary for weekday_color. Is there a better choice?


from enum import Enum


class WeekDay(Enum):
    MONDAY = 'Monday'
    TUESDAY = 'Tuesday'
    WEDNESDAY = 'Wednesday'
    THURSDAY = 'Thursday'
    FRIDAY = 'Friday'


class Color(Enum):
    RED = 'Red'
    GREEN = 'Green'
    BLUE = 'Blue'


weekday_color = {
    WeekDay.MONDAY: Color.RED,
    WeekDay.TUESDAY: Color.GREEN,
    WeekDay.WEDNESDAY: Color.BLUE,
    WeekDay.THURSDAY: Color.GREEN,
    WeekDay.FRIDAY: Color.RED,
}


def main():
    today_input = 'Monday'
    today = WeekDay(today_input)
    today_color = weekday_color[today]
    print(today)
    print(today_color)


if __name__ == '__main__':
    main()
Design-wise, using an Enum is entirely appropriate -- they were added to make "magic" constants easier to work with by giving them informative debugging names and easier containment checks.

Using a dictionary to tie the two enums together is unnecessary in this case as it makes more sense, and is easily possible, to have color be an attribute of each Weekday.

Whether Color is itself an enum depends on what you will be doing with the colors:


do you need actual color values?
is the color value going to be sent to another system, or stay inside your own program?


Whether or not you have a separate Color enum, a good way to combine those two things is like so (shown using aenum1, but can be done with the stdlib Enum2:

from aenum import AutoNumberEnum

class WeekDay(AutoNumberEnum):
    _init_ = 'color'
    #
    MONDAY = 'red'
    TUESDAY = 'green'
    WEDNESDAY = 'blue'
    THURSDAY = 'green'
    FRIDAY = 'red'


and in use:

&gt;&gt;&gt; list(WeekDay)
[&lt;WeekDay.MONDAY: 1&gt;, &lt;WeekDay.TUESDAY: 2&gt;, &lt;WeekDay.WEDNESDAY: 3&gt;, &lt;WeekDay.THURSDAY: 4&gt;, &lt;WeekDay.FRIDAY: 5&gt;]

&gt;&gt;&gt; WeekDay.MONDAY
&lt;WeekDay.MONDAY: 5&gt;

&gt;&gt;&gt; WeekDay['MONDAY']
&lt;WeekDay.MONDAY: 5&gt;

&gt;&gt;&gt; WeekDay.MONDAY.color
'red'


If you decide to use an enum for color, simply assign that instead of, for example, 'red'.



1 Disclosure:  I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum)  library.



2 The stdlib version (without the members):

class WeekDay(Enum):
    #
    def __new__(cls, color):
        value = len(cls.__members__) + 1
        obj = object.__new__(cls)
        obj._value_ = value
        obj.color = color
        return obj
    #
    # members here