List defining the list of variable of function in Python -
i want have function foo outputs function, list of variables depends on input list.
precisely:
suppose func function free variable t , 3 parameters a,gamma,x
example: func = lambda t,a,gamma,x: somefunction
i want define function foo, takes input list , outputs function. output function sum of func's, each func summand has parameters independent each other.
depending on input list variables of outputs changes in following:
if entry of list 'none' output function 'gains' variable , if entry of list float 'fixes' parameter.
example:
li=[none,none,0.1] g=foo(li) gives same output as
g = lambda t,a,gamma: func(t,a,gamma,0.1)
or
li=[none,none,0.1,none,0.2,0.3] g=foo(li) gives same output as
g = lambda t,a1,gamma1,a2:func(t,a,gamma,0.1)+func(t,a2,0.2,0.3)
note: order in list relevant , behaviour wanted.
i don't have clue on how that...
i first tried build string, depends on inout list , execute it, surely not way.
first, partition parameters li chunks. use iterator either next function parameters *args, if value in chunk none, or provided value parameters chunk.
def foo(li, func, num_params): chunks = (li[i:i+num_params] in range(0, len(li), num_params)) def function(t, *args): result = 0 iter_args = iter(args) chunk in chunks: actual_params = [next(iter_args) if x none else x x in chunk] result += func(t, *actual_params) return result return function example:
def f(t, a, b, c): print t, a, b, c return + b + c func = foo([1,2,none,4,none,6], f, 3) print func("foo", 3, 5) output:
foo 1 2 3 # first call f foo 4 5 6 # second call f 21 # result of func
Comments
Post a Comment