Encapsulation
Encapsulation is another programming pattern that often applies to modules and packages. Using encapsulation, you have a thing—for example, a color, a customer, or a currency—that you need to store data about, but you hide the representation of this data from the rest of your system. Rather than make the thing available directly, you provide functions for setting, retrieving, and manipulating the thing's data.
To see how this works, let's look back at a module we wrote in the previous chapter. Our chart.py
module lets the user define a chart and set the various pieces of information about it. Here is a copy of the code that we wrote for this module:
def new_chart(): return {} def set_title(chart, title): chart['title'] = title def set_x_axis(chart, x_axis): chart['x_axis'] = x_axis def set_y_axis(chart, minimum, maximum, labels): chart['y_min'] = minimum chart['y_max'] = maximum chart['y_labels'] = labels def set_series_type(chart, series_type...