How to Chain Multiple Function Calls in Python?

4.2/5 - (4 votes)

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()))
# 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)
# 1

But you can also chain 5 functions:

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

Or even 100 functions:

inc_100 = chain(0, *[increment] * 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!