Elements of Functional Programming in Python
By: Parul Pandey
Tags: Python, Lists, Functional Programming, Data Science, Programming

“Object-oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.” : Michael Feathers

There are multiple programming languages in the world, and so are the categories in which they can be classified. A programming paradigm is one such way which tries to classify programming languages based on their features or coding style. A programming paradigm is essentially a style or a way of programming.

Most of the times, we understand Python as an object-oriented language, where we model our data in the form of classes, objects, and methods. However, there also exist several alternatives to OOP, and functional programming is one of them.

Here are some of the conventional programming paradigms prevalent in the industry:

Conventional programming paradigms: Content Source Wikipedia

Functional Programming(FP)

The Functional Programming Paradigm

As per Wikipedia, Functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data.

The above definition might sound confusing at first, but it essentially tries to put forward the following aspects:

FP relies on functions, and everything is done using functions. Moreover, FP focuses on defining what to do, instead of performing some action. The functions of this paradigm are treated as first-class functions. This means functions are treated like any other objects, and we can assign them to variables or pass them into other functions.

what to do, instead of performing some action. The functions of this paradigm are treated as functions. This means functions are treated like any other objects, and we can assign them to variables or pass them into other functions. The data used in functional programming must be immutable, i.e. should never change. This means if we need to modify a data in a list, we need to create a new list with updated values rather than manipulating the existing one.

i.e. should never change. This means if we need to modify a data in a list, we need to create a new list with updated values rather than manipulating the existing one. The programs written in FP should be stateless . A stateless function has no knowledge about its past. Functional programs should carry out every task as if they are performing it for the first time. Simply put, the functions are only dependent on the data passed to them as arguments and never on the outside data.

. A stateless function has no knowledge about its past. Functional programs should carry out every task as if they are performing it for the first time. Simply put, the functions are only dependent on the data passed to them as arguments and never on the outside data. Laziness is another property of FP wherein we don’t compute things that we don’t have to. Work is only done on demand.

If this makes sense now, here is a nice comparison chart between the OOP and FP which will make things even more apparent.

Python provides features like lambda, filter, map, and reduce that can easily demonstrate the concept of Functional Programming. All the codes used in this article can be accessed from the associated Github Repository or can be viewed on my_binder by clicking the image below.