Functional Programming

Functional Programming with Python.

Beyond functools, install more-itertools, as the itertools docs suggest. toolz is a fuller Python functional programming library, and returns adds typed errors.

How to choose:

  • Partial application, decorators, and caching: functools
  • More iterator tools, the itertools recipes included: more-itertools
  • Composing functions into pipelines, currying, and dict helpers: toolz, or cytoolz for speed
  • Everyday helpers for collections, decorators, retries, and debugging: funcy
  • Errors and missing values as typed containers checked by mypy: returns

Listed in editorial order. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags
(Python standard library) Higher-order functions and operations on callable objects.
More routines for operating on iterables, beyond itertools.
A collection of functional utilities for iterators, functions, and dictionaries. Also available as cytoolz for Cython-accelerated performance.
pytoolz/github.com/pytoolz/toolz /5,161 stars /
Fancy and practical functional tools.
Suor/github.com/Suor/funcy /3,506 stars /
A set of type-safe monads, transformers, and composition utilities.
dry-python/github.com/dry-python/returns /4,383 stars /

Functional Programming guide

functools is the standard library's module for higher-order functions, functions that act on or return other functions. Python's Functional Programming HOWTO calls partial() the most useful tool in the module: it fills in some of a function's arguments and gives you a new function. When you write a decorator, wrap its inner function with wraps, so the decorated function keeps its name and docstring. The same HOWTO finds many uses of reduce() clearer as a for loop.

The itertools docs point you to more-itertools for their recipes and many more. It collects building blocks beyond itertools, for grouping, windowing, lookahead, and more. The itertools recipes sit in its top-level package, so from more_itertools import flatten works.

toolz extends itertools and functools with functions that are composable, pure, and lazy, and its API follows Clojure's standard library. Each function takes and returns only iterables, dictionaries, and functions, so they compose to solve your own problems. pipe runs a value through a sequence of functions, like pipes in Unix. Stick with partial at first, and once it shows up several times in your code, switch to the toolz.curried namespace. toolz is a general-purpose library, and for data analytics its docs say a library built for it may serve you better. cytoolz implements the same API in Cython, as a drop-in replacement when you need more speed.

funcy is a collection of functional tools focused on practicality, inspired by Clojure and underscore. Next to sequence tools, it has collection functions that keep the type of a dict or set. It also has control flow helpers, like @retry and silent, and debugging helpers, like tap and log_calls. Many of its functions take a regex, a mapping, or a set where you'd pass a function.

returns puts results in typed containers: Maybe for None and Result for exceptions, plus IO for impure code and Future for async code. Its docs really recommend mypy, and typing only works correctly with its mypy plugin. So it fits projects that check types with mypy. Turn functions that raise into ones that return a Result with @safe, and chain the steps with flow, which its docs call the recommended way to write code with returns.

Mix functional style with the rest of your code: Python's HOWTO says functional-style programs usually give a functional-appearing interface and use non-functional features inside. For a plain map or filter, toolz's own docs call comprehensions more Pythonic. Learn a core set of functions; toolz says about a dozen covers most tasks, and the right word only helps when your readers know it too.

Know a project that belongs here?

Tell us what it does and why it stands out.