正如标题所解释的,我想知道是否有可能在调用Python函数时将单个参数传递给两个不同的形参。在例子:
def function(a, b):
return a + b
# The idea being that both a and b are assigned the value of 3 when the function is called
function(3)
给b
一个默认值。如果包含default,则替换为a
。
def f(a, b=None):
if b is None:
b = a
return a + b
如果你想概括和允许任意数量的args
>>> def sumify(*args):
... return sum(args)
...
>>> sumify(1,2,3)
6
>>> sumify(1)
1
>>> sumify()
0