2014-12-02 71 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