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

查看:51
本文介绍了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

我想知道变量参数列表的类型是 tuple 但这里不接受元组.

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天全站免登陆