Search code examples
pythonfunc

pythonic way to copy all function inputs


I am looking for a more elegant way in order to avoid having the following:

def funct(input1, input2, input3, input4, input5):
   input1_new = copy.copy(input1)
   input2_new = copy.copy(input2)
   input3_new = copy.copy(input3)
   input4_new = copy.copy(input4)
   input5_new = copy.copy(input5)

As I am currently having many files, using very extensive functions I was wondering if there is no more pythonic way to copy all inputs given in the function.

I am copying because of the stack and heap problem.

Thanks!


Solution

  • You can use * in your function's argument definition to receive all the inputs at once.

    def funct(*inputs):
        inputs_new = copy.copy(inputs)
    

    This way you will receive all the inputs in the inputs tuple and you can access them iteratively. For example, inputs[0] will be your input1 and etc.