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
argas 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
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
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
And you can also pass different types of functions:
chain(start, f1, f2, f3, ..., f100)
Assuming you have defined or imported functions
f100 in your code.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.