簡體   English   中英

使用不同的參數多次調用相同的函數python

[英]calling same functions in multiple times with different arguments python

兩種不同情況下的輸入文件:

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 

我不確定我是否理解這個問題。 但是據我了解,我認為您希望函數能夠接受可變數量的參數:

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

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

請讓我知道這是您要找的東西嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM