使用不同的参数python多次调用相同的函数

时间:2017-06-29 04:07:05

标签: python python-2.7

在两种不同情况下输入文件:

case1:

inputfile:

one-device:yes
number of device:01-05
first-device:
second-device:

Case2:
one-device:no
number of device:01-05
first-device:01-03
second-device:04-05

现在在案例1中,我只有一个开始和结束值,即01和05

我的职能是:

def func1(self, start, end):
     for i, x in enumerate (range(start, end)):
         do something
def func10 (self, start, end):
         do something

case 2: i have 2 different start and end value that is for first device 01-03 and 04-05.

案例1我的程序执行流程。

# if one-device input is yes
func1(arg1, agr2)
func2(arg1, agr2)
func3(arg1, agr2)
func4(arg1, agr2)
func5(arg1, agr2)
func6(arg1, agr2)
func7(arg1, agr2)
func8(arg1)
func9(arg1, agr2,arg3)
func10(agr1,arg2)
func11(arg1, agr2)
func12(arg1, agr2)
func13(arg1) 


#if one-device input is no.
#In  case2. i need to call two times functions func1 and func10.

我的程序执行流程与案例2类似:

# run two times with two diffewnt start and end values as per input in case 2
func1(arg1, agr2)
func1(arg1, agr2)


#flow continues as usal 
func2(arg1, agr2)
func3(arg1, agr2)
func4(arg1, agr2)
func5(arg1, agr2)
func6(arg1, agr2)
func7(arg1, agr2)
func8(arg1)
func9(arg1, agr2,arg3)
# run two times with two diffewnt start and end values as per input in case 2
func10(agr1,arg2)
func10(agr1,arg2)


#flow continues as usal 
func11(arg1, agr2)
func12(arg1, agr2)
func13(arg1)


Now question is i want to use if else statement to check one-device is yes or no.

if one-device  is yes  write case 1  flow

if one-device  is no  write case 1  flow 

if no write same flow with using for loop for func1 and func10 to run two  times.

If i use this method my code be be lot of duplicate .

I need help here 

1 个答案:

答案 0 :(得分:2)

我不确定我是否理解这个问题。但根据我的理解,我认为你希望函数能够接受可变数量的参数:

def f1(arg1, *args):
    print "first arg: ", arg1
    for arg in args:
        print "next arg:", arg

f1(1, "string", 3,4)

请告诉我这是您要找的?