This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
Both Object-Oriented Programming and Functional paradigms of programming operate on the same principle - Separation of Concerns. While Object-Oriented Programming does this by combining properties and their actions in a class, functional programming keeps properties and their actions separate and use functions to perform actions on objects.
Today, I explored the functional programming concepts as applied to Python and looked at how we can implement functional programming techniques to organize Python code.
In the developer universe, there is often a debate between which style of programming paradigm is better. While many have strong opinions about either writing pure Object Oriented code or pure functional code. The more pragmatic approach is to understand the benefits of both paradigms along with their caveats and embrace the best of both worlds whenever necessary.
Here are some of the important concepts of functional programming and their implementation in Python.
Pure Functions
Pure functions are the heart and soul of functional programming just like classes and objects are of Object-Oriented programming.
A pure function is a function that follows these two rules,
- If provided the same input, it will return the same output always.
- It does not result in any side-effects.
Side-Effects generally mean changing data outside the scope of the function such as printing to console, doing a network request, making a database change, accessing a global variable etc.
- def doubler(num):
- '''''
- Accepts a number and multiplies it by 2
- '''
- return num * 2
- print(doubler(5))
- def emoji_appender(list, emoji):
- '''''
- Accepts a list and a emoji and
- appends to every item of list
- '''
- new_list = []
- for item in list:
- new_list.append(str(item) + emoji)
- return new_list
- print(emoji_appender([1,2,3], '😀')) # ['1😀', '2😀', '3😀']
- print(emoji_appender(['alpha','beta','gamma'], '😀'))
- # ['alpha😀', 'beta😀', 'gamma😀']
What are the benefits of Pure Functions?
A pure function should ideally perform only a single, specific action. Since pure functions always return the same output, provided the same input, they are very easy to test as they are predictable. This predictability feature always allows several pure functions to be run in parallel as they don’t have in side-effects. It makes code easier to read and understand.

Join the conversation! Your thoughts help the community grow.