liveBook · Manning

Enum

As explained in Software Design for Python Programmers

An enumeration type defines a fixed set of named constant values.

Use an enumeration when an attribute must accept a limited, named set of values. A book genre can be ADVENTURE, CLASSICS, DETECTIVE, FANTASY, HISTORIC, HORROR, ROMANCE, or SCIFI. Compared with strings, enumeration constants avoid case and spelling problems and make runtime comparisons efficient. section 2.3.2

Book genres in Python

Define Genre with Enum, then pass Genre.ADVENTURE or Genre.CLASSICS into Attributes. The catalogue stores the attributes and delegates matching to them, so genre changes remain encapsulated rather than leaking into Catalogue. section 2.3.2

Listing 2.4 (Program 2.2 Books-2): attributes.py
from enum import Enum

class Genre(Enum):    #1
    UNSPECIFIED = 0
    ADVENTURE   = 1
    CLASSICS    = 2
    DETECTIVE   = 3
    FANTASY     = 4
    HISTORIC    = 5
    HORROR      = 6
    ROMANCE     = 7
    SCIFI       = 8

    def __str__(self): return self.name.lower()

class Attributes:

    @staticmethod
    def _equal_ignore_case(target_str, other_str):    #2
        if len(target_str) == 0: 
            return True
        else:
            return target_str.casefold() == other_str.casefold()

    def __init__(self, title, last, first, year, genre):
        self._title = title
        self._last  = last
        self._first = first
        self._year  = year     #3
        self._genre = genre     #3

    @property
    def title(self): return self._title

    @property
    def last(self): return self._last

    @property
    def first(self): return self._first

    @property
    def year(self):  return self._year

    @property
    def genre(self): return self._genre

    def is_match(self, target_attrs):    #4
        return (     
                Attributes._equal_ignore_case(target_attrs.title, 
                                              self._title)
            and Attributes._equal_ignore_case(target_attrs.last,  
                                              self._last)
            and Attributes._equal_ignore_case(target_attrs.first, 
                                              self._first)
            and (    (target_attrs.year  == 0)
                  or (target_attrs.year  == self._year))
            and (    (target_attrs.genre == Genre.UNSPECIFIED)
                  or (target_attrs.genre == self._genre))
        )

    def __str__(self):
        return (f"{{TITLE: '{self._title}', LAST: '{self._last}', "
                f"FIRST: '{self._first}', YEAR: {self._year}, "    #5
                f"GENRE: {self._genre}}}")     #5

The listing shows named Genre constants, their numeric values, and a string representation used by the surrounding book-catalogue example. section 2.3.2

How Software Design for Python Programmers covers it

How deep it goes
Defines enumeration constants and contrasts them with strings, but does not explore enumeration internals.
Examples
Uses book genres such as ADVENTURE and CLASSICS in Python catalogue construction and search examples.
What you can do after
Apply it via Python's Enum class to represent a bounded attribute safely.
How it connects
Connects enumeration with strings, encapsulation, delegation, and loose coupling.

Also explained in

Compare how 23 books explain enum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.