Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Chain Multiple Function Calls in Python?

#1
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.

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.

Join the free webinar now!

The post How to Chain Multiple Function Calls in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/01/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Customize Multiple Subplots in Matplotlib xSicKxBot 0 12 03-02-2021, 07:29 AM
Last Post: xSicKxBot
  [Tut] How to Compare Rpm Versions in Python? xSicKxBot 0 13 03-01-2021, 07:10 PM
Last Post: xSicKxBot
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 25 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 41 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 33 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 40 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python repr() Function — A Helpful Guide with Example xSicKxBot 0 40 02-15-2021, 02:40 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 35 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 39 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 45 02-04-2021, 08:21 PM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Sound Pack for Game Rust (Soundboard fil...
Last Post: SickProdigy
Today 02:51 AM
» Replies: 0
» Views: 3
Play Custom Songs for Instruments / Песн...
Last Post: SickProdigy
Today 02:49 AM
» Replies: 0
» Views: 6
Microsoft - New Group Transcribe app tra...
Last Post: xSicKxBot
Today 01:52 AM
» Replies: 0
» Views: 2
News - Soapbox: The Nintendo Switch Was ...
Last Post: xSicKxBot
Today 01:51 AM
» Replies: 0
» Views: 2
[Tut] Best LLC Services for Your Freelan...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 5
(Indie Deal) PAW Patrol, Ben10, Dead in ...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 3
(Free Game Key) Wargame: Red Dragon - Fr...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 3
Flutter 2 Release Adds Desktop and Web S...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 4
Microsoft - Meet the 2021 Imagine Cup Wo...
Last Post: xSicKxBot
Yesterday 07:38 PM
» Replies: 0
» Views: 2
News - Review: G-Darius HD – A True Shmu...
Last Post: xSicKxBot
Yesterday 07:37 PM
» Replies: 0
» Views: 3

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020