passing functions and its arguments to another function

后端 未结 3 1683
醉话见心
醉话见心 2021-01-16 15:38

I have tree types of sub-functions:

  • one without any parameters (arguments),
  • second with one parameter
  • third with multiple parameters (tuple
相关标签:
3条回答
  • 2021-01-16 15:53

    Post of R Nar is exactly what I tried to achieve. I added additional if statement to verify if kwarg is an integer or a tuple. Thanks that it is not neccessary to put all **kwargs in a tuple. Thank you guys for help!

    def function_results_sum(*args, **kwargs):
        result = 0
        for func in args:
            if func.__name__ in kwargs:
                if type(kwargs[func.__name__]) == int:
                    result += func(kwargs[func.__name__])
                elif type(kwargs[func.__name__]) == tuple:
                    result += func(*kwargs[func.__name__])
            else:
                result += func()
        return result
    
    result = function_results_sum(no_arg, ident, mult, ident=2, mult=(3, 4))
    print(result)
    
    0 讨论(0)
  • 2021-01-16 16:06

    by goolging 'python determine number of args for passed function' I found How can I find the number of arguments of a Python function?

    I'm pretty sure you don't want the **kwars key, value syntax so I use a func_list regular arg and *args

    from inspect import signature
    
    
    def function_results_sum(func_list, *args):
    
        arg_gen = (e for e in args)
    
        return sum([func(*(next(arg_gen)
                           for _ in range(len(signature(func).parameters))))
                   for func in func_list])
    
    function_results_sum([no_arg, ident, mult], 7,8,9)
    84  
    

    the input can be made flatter by parsing *args for Functions and (presumed) arguments (anything not Type Function)

    from inspect import signature
    import types
    
    
    def function_results_sum(*args):
    
        func_gen = (e for e in args if isinstance(e, types.FunctionType))
    
        arg_gen = (e for e in args if not isinstance(e, types.FunctionType))
    
        return sum(func(*(next(arg_gen)
                           for _ in range(len(signature(func).parameters))))
                   for func in func_gen)
    
    function_results_sum(no_arg, ident, mult, 10,6,90)
    555
    

    order of functions and order of args are important, but separately, can be interleaved:

    function_results_sum(no_arg, 10, ident, 6, 90, mult)
    Out[399]: 555
    
    0 讨论(0)
  • 2021-01-16 16:11

    Something like this would work:

    def no_arg():
        return 5
    
    def one_arg(x):
        return x
    
    def multiple_args(x, y):
        return x * y
    
    def function_results_sum(*args, **kwargs):
        result = 0
        for func in args:
                result += func(*kwargs[func.__name__])
        return result
    

    Output:

    function_results_sum(
        no_arg, one_arg, multiple_args,
        no_arg=(),
        one_arg=(23, ),
        multiple_args=(1,5))
    
    33
    

    The only difference between what you are asking is that you have to put args in a tuple to then unpack as args to pass in later.

    If you dont want to have to supply anything for no argument functions, you can double check if the func name is in kwargs:

    def function_results_sum(*args, **kwargs):
        result = 0
        for func in args:
            if func.__name__ i kwargs:
                result += func(*kwargs[func.__name__])
            else:
                result += func()
        return result
    
    0 讨论(0)
提交回复
热议问题