2014-12-02 73 views
0

條件語句比方說,我有一個不接受3個可選參數的函數:的Python:可選功能參數

def function(arg1=0, arg2=0, arg3=0) 

什麼是處理功能,這取決於參數傳遞中條件句最徹底的方法?

現在我只有:

def function(arg1=0, arg2=0, arg3=0) 
    if arg1 !=0 and arg2 !=0 and arg3 != 0: 
     # do stuff with all three args 
    elif arg1 !=0 and arg2 != 0: 
     # do stuff with arg1 and arg2, etc... 

爲了擴大在此,如果我的功能,可以採取什麼樣的5個參數?爲所有可能的組合做條件似乎是一種阻力。我可以不做別的檢查哪些參數已經傳遞給函數嗎?

更新: 基於一些反饋,我想我只是在實際解釋我在做什麼。我需要根據他們什麼時候從學校畢業(高中,大學,研究生課程等)來估計某人的年齡。我可以有多個年去了,其實我可以有多個年各高中,大學的,等

所以,一個例子是:

def approx_age(highSchool=0, college=0): 
    this_year = date.today().year 
    if (highSchool != 0) and (college != 0): 
     hs_age = (this_year - highSchool) + 18 
     college_age = (this_year - college) + 21 
     age_diff = abs(hs_age - college_age) 
     if age_diff == 0: 
      return hs_age 
     elif return (hs_age + college_age)/2 
    elif highSchool != 0: 
     hs_age = (this_year - highSchool) + 18 
     return hs_age 
    elif college != 0: 
     college_age = (this_year - college) + 21 
     return college_age 

事情只打算獲得從這裏更加複雜......

+0

您有所有可能的組合,或參數只是數量條件通過呢? – Marcin 2014-12-02 07:11:33

+0

在大多數情況下,'arg!= 0'等於'if arg',實際上。 – favoretti 2014-12-02 07:12:22

+0

如果第一個'if'子句爲真,則第二個'if'子句將爲真。你想要那個嗎? – Sriram 2014-12-02 07:18:39

回答

0

我想你可以熬下來到這個利用基本的數學

def function(arg1=0, arg2=0, arg3=0): 
    if arg1 * arg2 * arg3 != 0: 
     # do stuff with all three args 
    if arg1 * arg2 != 0: 
     # do stuff with arg1 and arg2, etc... 
+0

只有當參數保持整數時,這纔有效。 – favoretti 2014-12-02 07:13:33

2

爲什麼與價值0和使用檢查argumnet是pythonic。 條件語句檢查語句Truth value如果它是True,則條件繼續else切換到下一個條件。

In [112]: bool(0) 
Out[112]: False 

In [113]: bool(None) 
Out[113]: False 

因此,如果參數值0你不需要用0蟒蛇匹配它selfly說明吧。

def function(arg1=0, arg2=0, arg3=0) 
    if arg1 and arg2 and arg3: 
     # do stuff with all three args 

    elif arg1 and arg2: 
     # do stuff with arg1 and arg2, etc... 
    .. 
    .. 

    else: 
     #do something 

None太:

def function(arg1=None, arg2=None, arg3=None) 
     if arg1 and arg2 and arg3: 
      # do stuff with all three args 
+0

謝謝。如果有很多潛在的論點,你能否看看我在OP中增加了哪些內容? – AutomaticStatic 2014-12-02 07:17:20

+0

@AutomaticStatic'我不能做別的事情來檢查哪些參數已經傳遞給函數嗎?'我沒有明白你想在這裏做什麼? – 2014-12-02 07:30:59

0
def function(arg1=None, arg2=None, arg3=None) 
    if (arg1 and arg2 and arg3): 
     # do stuff with all three args 
    if (arg1 and arg2): 
     # do stuff with arg1 and arg2, etc... 

我覺得None會更好地工作,你的情況。

>>> if (1 and 1 and 1): 
...  print True 
... 
True 
>>> if (0 and 0 and 0): 
...  print True 
... 
... 
>>> if (None and None and None): 
...  print True 
... 

所以你會得到第一個條件的所有3個ARGS那麼只有它會得到你的if scope內。

與第二種情況相同,如果arg1和arg2都不是None0,它將進入if範圍內。

0

我不能肯定,因爲我不知道你的功能與所有這些論點做,但它好像你應該使用**kwargs

def function(**kwargs): 
    for key, value in kwargs.iteritems(): 
     do_the_thing() 

**語法意味着你的函數將接受任何和所有關鍵字參數,並將它們粘在一個字典中。kwargs是一個常見的名稱給這個字典。您也可以使用*args來獲取位置參數數組。

哦也 - 如果你最終檢查的參數感實性,請考慮使用all

if all([arg1, arg2, arg3]): 
+0

如何檢測兩個參數是0還是哪些,或者3個參數是0等等,還是隻給出一個,但是哪一個? – Marcin 2014-12-02 07:24:37

+0

它不;它對調用者實際傳遞的參數進行操作。 – 2014-12-02 07:26:07

+0

是的,似乎OP想知道哪些參數傳遞,如果他們是0或不,並做一些取決於此的東西。 – Marcin 2014-12-02 07:27:15