Skip to main content
 首页 » 编程设计

python之忽略传递的参数

2025年05月04日51Leo_wl

是否可以忽略方法中传递的/过度给定的参数?

这是我想做的事情的一个例子:

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)