Python passing multiple parameters to functions

Hi,

I am a beginner in python programming. In my python script have a main function which calls several other functions. The main function gets its input by reading lines from a input text file. I call the main function for every line in input text file through a loop.

def main(line):
var1 = line.split('=')[1]
var2 = line.split('=')[2]
var3 = line.split('=')[3]
var4 = line.split('=')[4]
var5 = line.split('=')[5]
var6 = line.split('=')[6]

subfunc1(var2,var3,var4,var5,var6)
subfunc2(var1,var2,var3,var4,var5)
subfunc3(var1,var2,var3,var4,var5)
subfunc4(var1,var2,var4,var5,var6)
subfunc5(var1,var2,var3,var4,var5)
subfunc6(var1,var2,var4,var5,var6)

#Main
#input data is read as list with name inputlines
 
for line in inputlines:
main(line)

How can I avoid passing multiple variables to the subfunction and at same time how can I avoid the split operation in all these subfunctions.

Would you be willing to post a few lines from the input text? Also, if you do not mind, could you show what any of those subfuncx functions do. What you have posted is not enough to provide a meaningful suggestion without a lot of guessing.
The more you can show to understand what you are trying to do the faster that it would be for you to get an answer.

1 Like
cat input.txt

element1=element2=abc=def=zyz=12345=6789=100000
element2=element1=abc=def=zyz=12345=6789=100000
element3=element4=abc=def=zyz=12345=6789=100000

input.txt is just a delimited file with text required for my script to process. My subfuncx functions would require entire line and each of these functions have different objectives with each of these variables. Hence the best way is to pass the entire line to each of these subfunctions but that would mean that I will have to write split for every variable I require. Is there any better way to do that?

Why don't you

  • create a function for the split to be called from within any of the other functions
  • split into an array and call the functions with that array
    ?
1 Like

Thanks Rudic.

If I create a function I need to return several values. I think that would not look nice. Probably I would do that only as calling the function with the array also would be just like what I have written already.

Functions should do one and preferable one thing only. An indication that a function is doing too much is the amount of passed arguments that it has. Thus said, going along with your design choice of subfuncX, here's an example that might show some way of handling the passing of arguments, and function calls.

#!/usr/bin/env python

def funct1(a):
    print("funct1 showing argument {}".format(a[0]))


def funct2(a):
    print("funtct2 showing arguments {} and {}".format(a[0], a[1]))


def funct3(a):
    print("funct3 showing arguments {}, {} and {}".format(a[0], a[1], a[2]))


funct_cluster = [ funct1, funct2, funct3 ]

def main(ar):
    for funct in funct_cluster:
        funct(ar)

if __name__ == "__main__":
    with open('input.txt', 'r') as f:
        for line in f:
            arguments = line.rstrip().split('=')
            main(arguments)
1 Like

Thanks Aia. This is an elegant solution to my problem.