Programmatically define a new function from a list of other functions

lnNoam

I have:

def f(x, y): return x**2 + y**2
def g(x, y): return x**3 + y**3
def h(x, y): return x**4 + y**4

And I'd like to make a new function:

def J(x, y):
    return f(x,y)*g(x,y)*h(x,y)

However, I am unable to find a way to do this programmatically. That is, take something like:

myFunctions = [f,g,h]

and return a new function J which returns the product of f, g and h.

Another wrinkle is that while f, g and h will always have an identical number of arguments, that number could change. That is, they could all have could have five arguments instead of two.

Desired behaviour:

print(J(2, 2)) # 4096

EDIT

The number of functions in myFunctions is also arbitrary. I thought this was implied in my question, but upon rereading it I see that I did not make that at all clear. My apologies.

sepp2k

A function can accept arbitrarily many arguments using * like this:

def J(*args):

This will store all of J's arguments in the list args. That list can then be converted back into multiple arguments to call other functions like this:

def J(*args):
  return f(*args) * g(*args)

This solves the problem of the number of arguments changing. So now let's handle the fact that there can be arbitrarily many functions. First we need to call the function in your list. We can just do that by iterating over them and using ():

def J(*args):
  return [func(*args) for func in myFunctions]

This will return a list of the functions' return values. So all we need now is to get the product of a collection:

from functools import reduce
from operator import mul

def product(numbers):
  return reduce(mul, list, 1)

def J(*args):
  return product(func(*args) for func in myFunctions)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Define a list of functions from a generic function

From Dev

How to use Reduce to create a new function from a list of functions?

From Dev

Maxima: define a function that returns a random integer in a range, such that the value is distinct from another value or a list of other values

From Dev

Create new forms programmatically from for function

From Dev

How to define a variable that is a function pointer that can assign other functions in c?

From Dev

Create new functions using a list of functions and list of function parameters to Be Passed

From Dev

javascript Function constructed with new Function cannot call other functions

From Dev

Call other functions from a callback function

From Dev

Move items from one sortable connected list to the other programmatically

From Dev

Creating a new list from the means of a list of a function

From Dev

How to construct a function from a list of functions and a value?

From Dev

How to call random function from list of functions?

From Dev

how to call other functions in flat list render function?

From Dev

Programmatically evaluate a list of functions in Clojure

From Dev

Define a first-order function that combines other first-order functions

From Dev

python use output from one function in other functions without calling all the other functions

From Dev

Return always new list from a function or not?

From Dev

Python: How to use named variables from one function in other functions

From Dev

Creating and Passing of mxArray Data from other functions to Mex gateway function

From Dev

Call variables in construct function from other functions in Codeigniter

From Dev

Python - Use one variable from a function in other functions

From Dev

Define new multilevel list vs Define new list style

From Dev

Calling functions from other functions

From Dev

How to define new loop function

From Dev

How to define a new function in pdb

From Dev

How to define new loop function

From Dev

Function define a new object class

From Dev

Define a function with nested functions and default function

From Dev

R creating new list from other lists/dataframes

Related Related

  1. 1

    Define a list of functions from a generic function

  2. 2

    How to use Reduce to create a new function from a list of functions?

  3. 3

    Maxima: define a function that returns a random integer in a range, such that the value is distinct from another value or a list of other values

  4. 4

    Create new forms programmatically from for function

  5. 5

    How to define a variable that is a function pointer that can assign other functions in c?

  6. 6

    Create new functions using a list of functions and list of function parameters to Be Passed

  7. 7

    javascript Function constructed with new Function cannot call other functions

  8. 8

    Call other functions from a callback function

  9. 9

    Move items from one sortable connected list to the other programmatically

  10. 10

    Creating a new list from the means of a list of a function

  11. 11

    How to construct a function from a list of functions and a value?

  12. 12

    How to call random function from list of functions?

  13. 13

    how to call other functions in flat list render function?

  14. 14

    Programmatically evaluate a list of functions in Clojure

  15. 15

    Define a first-order function that combines other first-order functions

  16. 16

    python use output from one function in other functions without calling all the other functions

  17. 17

    Return always new list from a function or not?

  18. 18

    Python: How to use named variables from one function in other functions

  19. 19

    Creating and Passing of mxArray Data from other functions to Mex gateway function

  20. 20

    Call variables in construct function from other functions in Codeigniter

  21. 21

    Python - Use one variable from a function in other functions

  22. 22

    Define new multilevel list vs Define new list style

  23. 23

    Calling functions from other functions

  24. 24

    How to define new loop function

  25. 25

    How to define a new function in pdb

  26. 26

    How to define new loop function

  27. 27

    Function define a new object class

  28. 28

    Define a function with nested functions and default function

  29. 29

    R creating new list from other lists/dataframes

HotTag

Archive