Introduction to Functional Programming in Python
Python facilitates different approaches to writing code, and while an object-oriented approach is common, an alternative and useful style of writing code is functional programming.
The Map Function
While the ability to pass in functions as arguments is not unique to Python, it is a recent development in programming languages. Functions that allow for this type of behavior are called first-class functions. Any language that contains first-class functions can be written in a functional style.
There are a set of important first-class functions that are commonly used within the functional paradigm. These functions take in a Python iterable, and, like
sorted(), apply a function for each element in the list. Over the next few sections, we will examine each of these functions, but they all follow the general form of
The first function we'll work with is the
map() function. The
map() function takes in an iterable (ie.
list), and creates a new iterable object, a special
map object. The new object has the first-class function applied to every element.
Here's how we could use map to add
20 to every element in a list:
Note that it's important to cast the return value from
map() as a
list object. Using the returned
map object is difficult to work with if you're expecting it to function like a
list. First, printing it does not show each of its items, and secondly, you can only iterate over it once.
The Filter Function
The second function we'll work with is the
filter() function. The
filter() function takes in an iterable, creates a new iterable object (again, a special
map object), and a first-class function that must return a
bool value. The new
map object is a filtered iterable of all elements that returned
Here's how we could filter odd or even values from a list:
The Reduce Function
The last function we'll look at is the
reduce() function from the
functools package. The
reduce() function takes in an iterable, and then reduces the iterable to a single value. Reduce is different from
reduce() takes in a function that has two input values.
Here's an example of how we can use
reduce() to sum all elements in a list.
An interesting note to make is that you do not have to operate on the second value in the
lambda expression. For example, you can write a function that always returns the first value of an iterable:
Rewriting with list comprehensions
Because we eventually convert to lists, we should rewrite the
filter() functions using list comprehension instead. This is the more pythonic way of writing them, as we are taking advantage of the Python syntax for making lists. Here's how you could translate the previous examples of
filter() to list comprehensions:
From the examples, you can see that we don't need to add the lambda expressions. If you are looking to add
filter() functions to your own code, this is usually the recommended way. However, in the next section, we'll provide a case to still use the
Writing Function Partials
Sometimes we want to use the behavior of a function, but decrease the number of arguments it takes. The purpose is to "save" one of the inputs, and create a new function that defaults the behavior using the saved input. Suppose we wanted to write a function that would always add 2 to any number:
add_two function is similar to the general function, $f(a,b) = a + b$, only it defaults one of the arguments ($a = 2$). In Python, we can use the
partial module from the
functoolspackage to set these argument defaults. The
partial module takes in a function, and "freezes" any number of args (or kwargs), starting from the first argument, then returns a new function with the default inputs.
Partials can take in any function, including ones from the standard library.
In this post, we introduced the paradigm of functional programming. We learned about the lambda expression in Python, important functional functions, and the concept of partials. Overall, we showed that Python provides a programmer with the tools to easily switch between functional programming and object-oriented programming.
If you've enjoyed functional programming, our newest course: Building a Data Pipeline uses functional programming concepts to build a data pipeline. In this course, you'll find more advanced Python concepts, examples of good API design, and a final project that uses your own data pipeline built from scratch!
Original. Reposted with permission.
- Web Scraping Tutorial with Python: Tips and Tricks
- Moving from R to Python: The Libraries You Need to Know
- Data Structures Related to Machine Learning Algorithms
Top Stories Past 30 Days