How to Chain Multiple Function Calls in Python?

Problem Formulation

Given a number of functions f1, f2, …, fn. How to chain the function calls so that the output of the i-th function is used as input for the (i+1)-th function?

f1()   ---> output f1() is input of f2()   ---> output f2() is input of f3() ...    ---> ... is input of fn()

Simple Solution: Chaining

Here’s the most Pythonic and simple solution to the problem of chaining function calls:

def f1():
    return 'f1'


def f2(arg):
    return arg + ' f2'


def f3(arg):
    return arg + ' f3'


# Chain the outputs into the next functions
result = f3(f2(f1()))
print(result)
# f1 f2 f3
  • You define the input arguments of each function within the parentheses.
  • You can set the input to any name and access the input within the function body using the argument name. In our case, we use the name arg as the input arguments’ names.
  • Then you use the outer function f3(...) that is the last function to be executed. As input, you use the result of f2(...). As input for this function, you use the result of f1(). This way, you can chain three or more functions by using the pattern f3(f2(f1())).

Advanced Solution: Pythonic Meta Function for an Arbitrary Number of Function Chains

If you have many functions (more than, say, ten functions), the previously shown method becomes burdensome. In this case, I’d recommend using a functional approach to function chaining—I know, it’s meta!

def chain(start, *funcs):
    res = start
    for func in funcs:
        res = func(res)
    return res

The chain() function takes an initial seed called start. It then goes over all functions passed as arguments and passes the current result as an input to the function. Then, it overwrites the current result with the function output—chaining the output of one function into the next function’s input arguments.

You can now chain one function:

def increment(arg):
    return arg + 1


inc_1 = chain(0, increment)
print(inc_1)
# 1

But you can also chain 5 functions:

inc_5 = chain(0, increment, increment, increment, increment, increment)
print(inc_5)
# 5

Or even 100 functions:

inc_100 = chain(0, *[increment] * 100)
print(inc_100)
# 100

This is the same as increment(increment(...increment(increment(0))...)).

And you can also pass different types of functions:

chain(start, f1, f2, f3, ..., f100)

Assuming you have defined or imported functions f1 to f100 in your code.

Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source xkcd

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!