FUNCTIONAL-STYLE PROGRAMMING
Like other popular languages, such as Java and C#, Python is not a purely functional language. Rather, it offers “functional-style” features that help you write code which is less likely to contain errors, more concise and easier to read, debug and modify.
Functional-style programs also can be easier to parallelize to get better performance on today’s multicore processors. The chart below lists most of Python’s key functional-style programming capabilities and shows in parentheses the chapters in which we initially cover many of them.
![]() |
| By JuTT BaDshaH |
We cover most of these features throughout the book—many with code examples and others from a literacy perspective. You’ve already used list, string and builtin function range iterators with the for statement, and several reductions (functions sum, len, min and max). We discuss declarative programming, immutability and internal iteration below.
What vs. How
As the tasks you perform get more complicated, your code can become harder to read, debug and modify, and more likely to contain errors. Specifying how the code works can become complex.
Functional-style programming lets you simply say what you want to do. It hides many details of how to perform each task. Typically, library code handles the how for you. As you’ll see, this can eliminate many errors.
Consider the for statement in many other programming languages. Typically, you must specify all the details of counter-controlled iteration: a control variable, its initial value, how to increment it and a loop-continuation condition that uses the control variable to determine whether to continue iterating. This style of iteration is known as external iteration and is errorprone. For example, you might provide an incorrect initializer, increment or loopcontinuation condition. External iteration mutates (that is, modifies) the control variable, and the for statement’s suite often mutates other variables as well. Every time you modify variables you could introduce errors.
Functionalstyle programming emphasizes immutability.
That is, it avoids operations that modify variables’ values. We’ll say more in the next chapter.
Python’s for statement and range function hide most counter-controlled iteration details. You specify what values range should produce and the variable that should receive each value as it’s produced. Function range knows how to produce those values. Similarly, the for statement knows how to get each value from range and how to stop iterating when there are no more values. Specifying what, but not how, is an important aspect of internal iteration—a key functionalstyle programming concept.
The Python builtin functions sum, min and max each use internal iteration. To total the elements of the list grades, you simply declare what you want to do—that is, sum(grades). Function sum knows how to iterate through the list and add each element to the running total. Stating what you want done rather than programming how to do it is known as declarative programming.
Pure Functions
In pure functional programming language you focus on writing pure functions. A pure function’s result depends only on the argument(s) you pass to it. Also, given a particular argument (or arguments), a pure function always produces the same result.
For example, builtin function sum’s return value depends only on the iterable you pass to it. Given a list [1, 2, 3], sum always returns 6 no matter how many times you call it. Also, a pure function does not have side ef ects. For example, even if you pass a mutable list to a pure function, the list will contain the same values before and after the function call. When you call the pure function sum, it does not modify its argument.
View code image
In [2]: sum(values)
Out[2]: 6
In [3]: sum(values) # same call always returns same result
Out[3]: 6
In [4]: values
Out[5]: [1, 2, 3]
In the next chapter, we’ll continue using functional-style programming concepts. Also, you’ll see that functions are objects that you can pass to other functions as data.
