Call two or more functions with one args
Call two or more functions with one args. one last Day إلى pythondevowner قبل 7 أيام التفاصيل #While reading about functions in mathematics, a figure representing the sum of two functions caught my attention. #I found it would be useful to add it in the next version of Python. Of course, what is meant by collecting the two functions in mathematics is to collect the results of the two functions, but here I mean to collect the two or more functions on one args. #If all the added functions have the same parameter and the same number of them, then the form x = multi (fun1,func2)(3.5) will be very useful def multi (a,b,s): #func to return multi func with one arg or multi args #Operations are performed on all parematers that represent functions using one args, with the condition that all functions accept the same number of variables #In this case, it is just a function that we want to develop to avoid possible errors and build several scenarios #It has to be something like that # x = multi (func1,func2)(3.5) # and return list of func results or None for func with None return # print(x[0], x[1]) a1 = a(s) b1 = b(s) return ([a1,b1]) def a (s): #print(s) return s def b (s): #print(s+1) return s+1 x = multi(a,b,2) print(x[0], x[1])
On Sat, Nov 6, 2021 at 1:06 AM one last Day
Call two or more functions with one args. one last Day إلى pythondevowner قبل 7 أيام التفاصيل
#While reading about functions in mathematics, a figure representing the sum of two functions caught my attention. #I found it would be useful to add it in the next version of Python. Of course, what is meant by collecting the two functions in mathematics is to collect the results of the two functions, but here I mean to collect the two or more functions on one args. #If all the added functions have the same parameter and the same number of them, then the form x = multi (fun1,func2)(3.5) will be very useful
def multi (a,b,s): #func to return multi func with one arg or multi args #Operations are performed on all parematers that represent functions using one args, with the condition that all functions accept the same number of variables
#In this case, it is just a function that we want to develop to avoid possible errors and build several scenarios #It has to be something like that # x = multi (func1,func2)(3.5) # and return list of func results or None for func with None return # print(x[0], x[1]) a1 = a(s) b1 = b(s) return ([a1,b1])
def a (s): #print(s) return s def b (s): #print(s+1) return s+1
x = multi(a,b,2) print(x[0], x[1])
Have you tried list comprehensions? x = [f(2) for f in (a,b)] ChrisA
On Fri, Nov 05, 2021 at 03:53:56AM 0700, one last Day wrote:
Call two or more functions with one args. [...]
This is like a version of map() except that instead of calling one function with a bunch of arguments, it calls many arguments with one set of arguments. It seems to be related to the "apply()" functional programming function too. I haven't been able to find a standard name for this functional programming idiom, but it does seem to be a moderately common thing. In Clojure: https://stackoverflow.com/questions/67007139/howdoirunmultiplefunctions... Python: https://stackoverflow.com/questions/35858735/applymultiplefunctionswithm... Apply multiple functions to the same argument in functional Python: https://www.py4u.net/discuss/203479 I think the simplest eager implementation would be: def apply(functions, *args, **kwargs): results = [None]*len(functions) for i, f in enumerate(functions): results[i] = f(*args, **kwargs) return results If the functions have no sideeffects, this could be done in parallel, using threads, async, or process. Otherwise, it can be turned into a list comprehension: [f(*args, **kwargs) for f in functions] If the arguments include an iterator, then the first function call will consume the iterator. For example: # Works as expected >>> from math import prod >>> data = [1, 2, 3, 4] >>> [f(data, start=100) for f in (sum, prod)] [110, 2400] # Surprise! >>> data = iter([1, 2, 3, 4]) >>> [f(data, start=100) for f in (sum, prod)] [110, 100] So an iterator version would need to be written differently.  Steve
participants (3)

Chris Angelico

one last Day

Steven D'Aprano