Suppose I have a function
def oldfunction(arg1,arg2,arg3):
print(arg1,arg2,arg3)
I want to create a new function in which I have set the first argument to 10. The new function behaviour would be:
>>> new_function(20,"30")
10 20 '30'
One way to do this would be to use a lambda function:
>>> newfunction = lambda arg2,arg3: function(10,arg2,arg3)
>>> newfunction("ten",[10])
(10, 'ten', [10])
But, suppose I didn’t know in advance how many arguments “oldfunction” takes. I still want to create a new function that is the same as “oldfunction”, but with the first argument set, and all subsequent arguments left open.
Is there a way to do this?
The following example allows you to bind a 10 to the first argument position in oldfunction and then supply the remaining two arguments in *args. So, try this: