3.1 Writing pure functions
In Chapter 2, Introducing Essential Functional Concepts, we looked at pure functions. In this section, we’ll look at a common problem with non-functional programming: a function that has a reference to a global variable. When a global variable is assigned, the global
statement will be used. When a global variable is read, however, this is called a free variable, and there’s no obvious marker in the Python code.
Any references to values in the Python global namespace (using a free variable) is something we can rework into a proper parameter. In most cases, it’s quite easy. Here is an example that depends on a free variable:
global_adjustment: float
def some_function(a: float, b: float, t: float) -> float:
return a+b*t+global_adjustment
After refactoring the function, we would need to change each reference to this function. This may have a ripple effect through a complex application. We&...