How to Expanding tuples into arguments?

How to Expanding tuples into arguments?

Asked on November 10, 2018 in Python.
Add Comment


  • 3 Answer(s)

       Use myfun(*tuple) to Expanding tuples into arguments.

      Drawbacks:don’t use for identifiers builtin type names such as tuple, list, file, set, and so forth — it’s horrible practice and it will come back and bite at the least expect time, so just get into the habit of actively avoiding hiding builtin names with the own identifiers.

    Answered on November 10, 2018.
    Add Comment

    Use this code:

    myfun(1, *("foo", "bar"))
    
    

    Note:It is possible to  expand part of argument list.

    Answered on November 10, 2018.
    Add Comment

     Try this code to Expanding tuples into arguments:

    def run_argtup(func, argvalues):
        """
        Execute any functions with their arguments in tuple.
        :param func:
        :param argvalues:
        :return:
        """
        argnames = get_func_argnames(func)
        if len(argnames) != len(argvalues):
            raise ValueError("Length of args doens't match.")
        for argn, argv in zip(argnames, argvalues):
            exec('{}=argv'.format(argn))
        return eval('func(%s, %s)' % argnames)
    

    Answered on November 10, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.