忽略传递的参数
Ignore passed arguments
是否可以忽略方法中的 passed/overgiven 个参数?
这是我想做的事情的一个例子:
def event_handler(one, two):
print(one)
print(two)
在另一个地方:
event_handler(one, two, three)
我的意思是第三个参数是可选的。我已经在 Python Fiddle 中尝试过,但它无法正常工作。
将第三个设为默认参数
def event_handler(one, two, third = None):
然后做错误处理,因为是默认的,所以很容易。
def event_handler(one, two, third = None):
print(one)
print(two)
if (three):
print(three)
在 *args
中使用可变参数。
def f(*args): pass
像 args[0]
这样的列表访问变量,...
或者,
def f(required_arg1, required_arg2, *optional_args): pass
是否可以忽略方法中的 passed/overgiven 个参数?
这是我想做的事情的一个例子:
def event_handler(one, two):
print(one)
print(two)
在另一个地方:
event_handler(one, two, three)
我的意思是第三个参数是可选的。我已经在 Python Fiddle 中尝试过,但它无法正常工作。
将第三个设为默认参数
def event_handler(one, two, third = None):
然后做错误处理,因为是默认的,所以很容易。
def event_handler(one, two, third = None):
print(one)
print(two)
if (three):
print(three)
在 *args
中使用可变参数。
def f(*args): pass
像 args[0]
这样的列表访问变量,...
或者,
def f(required_arg1, required_arg2, *optional_args): pass