简体   繁体   English

Python——函数的可选参数

[英]Python -- optional arguments for a function

Say I want to build a function that either adds two numbers a and b , or subtracts them and adds a third number c and subtracts the fourth number d .假设我想构建一个函数,将两个数字ab相加,或者将它们相减并添加第三个数字c并减去第四个数字d I intend to specify which of the two operations is to be performed by an argument sum ;我打算指定两个操作中的哪一个将由参数sum执行; if this is True , the first operation is performed, if False , then the second operations is performed.如果为True ,则执行第一个操作,如果为False ,则执行第二个操作。 I would write this as:我会这样写:

def f(a, b, c, d, sum=True):
    if sum: return a+b
    else: return a-b+c-d

For example, f(1,2,3,4) returns 3 , while f(1,2,3,4,sum=False) returns -2 , as expected.例如, f(1,2,3,4)返回3 ,而f(1,2,3,4,sum=False)返回-2 ,正如预期的那样。 Clearly, though, c and d only need to be defined when sum=False .显然, cd只需要在sum=False时定义。 How do I do this?我该怎么做呢? I've tried setting c and d as *args, but I keep getting errors of the type "unsupported operand" or "positional argument follows keyword argument".我已经尝试将cd设置为 *args,但我不断收到“不支持的操作数”或“位置参数跟随关键字参数”类型的错误。

Use a default value of None for c and d:对 c 和 d 使用None的默认值:

def f(a, b, c=None, d=None, sum=True):
    if sum:
        return a+b
    else: 
        return a-b+c-d

This also allows you to add error-checking logic to your function - check whether c and d are present when sum is False:这还允许您将错误检查逻辑添加到您的函数中 - 当 sum 为 False 时检查 c 和 d 是否存在:

def f(a, b, c=None, d=None, sum=True):
    if sum: 
        return a+b
    else: 
        if None in (c, d):
            raise TypeError('f requires c and d args with parameter sum=False')
        return a-b+c-d

Others have given the way you can do it, but at a more fundamental level I'd ask why this is even the same method?其他人已经给出了你可以做到的方法,但在更基本的层面上,我会问为什么这甚至是相同的方法? Seems to me you have two different methods here, f_sum and f (or whatever), one takes two parameters and the other 4.在我看来,您在这里有两种不同的方法,f_sum 和 f(或其他),一个需要两个参数,另一个需要 4。

Or if it's a more complex operation and c and d are just additional parameters / attributes, you could default them to whatever the null value is eg for addition just default them to 0: 或者,如果它是一个更复杂的操作并且 c 和 d 只是附加参数/属性,您可以将它们默认为任何空值,例如,对于加法,只需将它们默认为 0:

 
 
 
 
  
  
  def f(a, b, c=0, d=0)
 
 
 

a+b-0+0 won't do anything so if these parameters are not provided the result will be identical to a+b without even needing a conditional or magical flag. a+b-0+0不会做任何事情,所以如果不提供这些参数,结果将与 a+b 相同,甚至不需要条件或魔法标志。 sorry missed that the second case was a - b and misread it as a +抱歉错过了第二种情况是 a - b 并将其误读为 +

The answer to your question is:你的问题的答案是:

def f(a, b, c=None, d=None, sum=True):
  if sum: return a + b
  else: return a - b + c - d

However, you could further simplify it to this:但是,您可以将其进一步简化为:

def f(a, b, c=0, d=0):
  return a - b + c - d

As the values of c and d depend on whether sum is true or false.因为 c 和 d 的值取决于sum是真还是假。

This solves your problem:这可以解决您的问题:

def f(a, b, c=0, d=0, sum=True):
    if sum: return a+b
    else: return a-b+c-d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM