Python passing multiple parameters to functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python passing multiple parameters to functions
# 1  
Old 05-08-2016
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.

Code:
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.
# 2  
Old 05-08-2016
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.

Last edited by Aia; 05-08-2016 at 05:45 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 05-09-2016
Code:
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?
# 4  
Old 05-09-2016
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
?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-09-2016
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.
# 6  
Old 05-09-2016
Quote:
Originally Posted by ctrld
Code:
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?
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.

Code:
#!/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)


Last edited by Aia; 05-09-2016 at 08:56 PM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 05-09-2016
Thanks Aia. This is an elegant solution to my problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Shell Programming and Scripting

Question on passing multiple parameters in if

Hi All, My target is to find the list of orphan processes running and i issue the below command with some exception ids. ps -ef | egrep -v "root|system|admin" | awk '{if ($3 == 1) print $1",\t"$2",\t"$3}' but this will exclude the process having the word 'root' and executing under different... (1 Reply)
Discussion started by: Arunprasad
1 Replies

5. Shell Programming and Scripting

Handling parameters in Shell Functions

Hi, Please help me with the below situation where I have to handle the parameters passed to a function in a unique way. Below is the code, which I am trying to execute. I basically want to pass the parameter to a function, where I am trying to get user input into array(s). I want to name... (7 Replies)
Discussion started by: bharath.gct
7 Replies

6. Shell Programming and Scripting

perl - passing hash references to functions

hi there I have the following script in which i have created a PrintHash() function. I want to pass to this function the reference to a hash (in the final code i will be passing different hashes to this print function hence the need for a function). I am getting an error Type of arg 1 to... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

7. Shell Programming and Scripting

Get the List of functions with modified parameters

Hi I have 2 files a.c and a.bak where I changed long to int using awk script. I want to get the list of functions whose parameters got modified for eg: fun ( long a, long b ) might be changed to fun ( int a, int b ) (1 Reply)
Discussion started by: Sivaswami
1 Replies

8. Shell Programming and Scripting

passing multiple files as parameters

hi all i am etl guy i have shell script that i use to do processing on file. the problem is that i want it to use on multiple files at same time is there any way of passing the file name since my all my filename start with samename like abc* p,ease let me know (4 Replies)
Discussion started by: er_zeeshan05
4 Replies

9. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

10. Shell Programming and Scripting

passing command line parameters to functions - sh

All, I have a sh script of the following tune: function a () { #functionality.. } function b () { #functionnlity.. } function check () { # this function checks for env and if all fine call build } function usage () { #sh usage details } function build () { #calls either a or b or... (5 Replies)
Discussion started by: vino
5 Replies
Login or Register to Ask a Question