I am struggling with the below exercise:
Arguments to particular functions should be passed to function_results_sum as keywords argumentsand it should look like FUNCTION_NAME=ARGUMENTS
The function_results_sum should return sum of all the results received after running each passing function with arguments
If the function has no arguments, the arguments shouldn't be passed to function_results_sum
If the function takes 1 argument, as keyword argument int will be passed to function_results_sum (for example one_arg_function_name=2)
If function takes more than 1 argument - tuple is expected to be passed (for example two_args_function_name=(1, 2) )
How it should work like: 1st example functions signatures: def no_arg() def one_arg(a) def multiple_args(a, b, c, e, f)
calling function_results_sum: function_results_sum( no_arg, one_arg, multiple_args, one_arg=23, multiple_args=(1, 2, 3, 4, 5) )
2nd example of how to call function_results_sum: function_results_sum( no_arg, one_arg, multiple_args, one_arg=-1245, multiple_args=(45, 65, 76, 123456, 111.222) )
! Use name attribute on the function object !
This is what I came up with, however I do not know why I get the result as the addresses of the cells where the outputs are stored:
<function ident at 0x00000288C0A72048> <function no_arg at
0x00000288C0A6BF28>
<function mult at 0x00000288C0A720D0>
My implementation:
def function_results_sum(*args, **kwargs):
return [*args]
def no_arg():
return 5
def ident(x):
return x
def mult(x, y):
return x * y
a = function_results_sum(ident, no_arg, mult, ident = 2, mult = (2, 3))
print(a)
Here's a hint that calls the one-argument function:
def function_results_sum(*args, **kwargs):
func = args[0]
function_name = func.__name__
parameter = kwargs[function_name]
return func(parameter)
def ident(x):
return x
a = function_results_sum(ident,ident=2)
print(a)
args
will contain a list of the functions to be called, and kwargs
contains the list of parameters using the function names as keys. See if you can figure out how to call the three types of functions.