pythonfunctionargumentspositional-argument

Specify an arbitrary argument in addition to *args in python


For a function with multiple arguments like this:

def f(a, b, c):
    return a + b + c

it is straightforward to define a new function as follows:

def g(a, *args):
    return f(a, *args)

here, a is the first argument of f(). What if I would like to specify the second argument of f(), namely:

def g(b, *args):
    # return f(???)

Solution

  • You can unpack some of the arguments from *args:

    def f(a, b, c):
        return a + b + c
    
    
    def g(b, *args):
        a,*rest = args
        return f(a, b, *rest)
    
    
    print(g(1,2,3))
    

    Output:

    6