在Python中变量参数列表默认值 [英] default values for variable argument list in Python

查看:486
本文介绍了在Python中变量参数列表默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在Python 3设置默认值变量参数列表?

Is it possible to set a default value for a variable argument list in Python 3?

是这样的:

def do_it(*args=(2, 5, 21)):
     pass

我不知道一个变量参数列表类型的元组,但没有元组在这里接受了。

I wonder that a variable argument list is of type tuple but no tuple is accepted here.

推荐答案

如果没有语法,那取决于你想要什么样的行为:

If not syntactically, then depending on what behavior you want:

def do_it(*args):
    if not args: args = (2, 5, 21)

def do_it(a=2, b=5, c=21, *args):
    args = (a,b,c)+args

应该这样做。

这篇关于在Python中变量参数列表默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆