The Dos and Don’ts of Functional Programming in Python

Written by Alton Zenon III
Published on Jun. 18, 2020
The Dos and Don’ts of Functional Programming in Python
Brand Studio Logo
Team of professional individuals working
shutterstock

There are right and wrong ways of coding in Python, especially when it comes to functional programming (FP). This school of development helps reduce bugs, make code maintenance easier and avoid side effects. And Python’s multi-paradigm and object-based nature makes FP feasible within it. But since the language wasn’t built specifically for that kind of coding, practicing FP in Python requires a certain level of nuance and skill. 

How a developer approaches this type of programming will depend largely on their experience with FP and the goals they’re trying to achieve. There are a number of resources devs can use to get more experience in Python FP, or to practice more advanced techniques. For instance, engineers new to this type of programming — or looking for more advanced techniques to improve their Python testing abilities — could find the test-based framework pytest beneficial. 

Ziyuan Chen, a full-stack developer at test administration software provider Examity, offers tips on how engineers can approach Python FP in a number of ways.

A piece of advice? Maintain perspective on how much FP within Python is actually possible and don’t get too attached to any single idea. 

 

Ziyuan Chen
Full-Stack Developer • Examity

What are the most important “dos” of functional programming in Python?

Functional programming has great synergy with unit testing. Small, pure functions are easy to run since all that’s needed is checking the inputs and outputs of the functions. The simplicity of Python and libraries like pytest make testing even easier to write. 

Get familiar with iterator, generator and itertool modules. An iterator is a foundational object for functional programming in Python. Understand how it works and its relationship with collection types. The itertool module contains some very useful and performant helper functions for iterators.

Think twice before using cool FP features like ‘fold’ or ‘currying.’”

 

What are the “don’ts” of functional programming in Python engineers should avoid?

Don’t be religious about functional programming in Python. It’s not a pure functional programming language like Haskell, and it can sometimes be difficult to write in a functional style like point-free. Blindly applying these styles for pureness might come with the cost of performance or readability, especially for people not familiar with functional programming. Think twice before using cool FP features like “fold” or “currying.” Sometimes the best solution might just be an imperative for “loop.”

What other advice do you have for Python developers who want to adopt functional programming?

Many functional languages have great type systems. While Python will probably never become a statically typed language, “Type hints (PEP 484)” might be a useful tool for catching bugs and improving readability.

Responses have been edited for length and clarity. Images via listed companies.

Hiring Now
Ampersand
AdTech • Big Data • Machine Learning • Sales • Analytics