如果变量默认值为None,则Python快捷方式将变量默认值设为另一个变量值

时间:2019-02-20 16:25:08

标签: python python-3.x

我有以下函数声明:

def set_data(obj=None, name='delp', type=None):

是否存在python快捷方式,如果类型为None,则类型等于'name'值(通过默认r)?

1 个答案:

答案 0 :(得分:3)

通常的习惯用法是:

def set_data(obj=None, name='delp', type=None):
    if type is None:
        type = name

(但是实际上不要使用type作为变量名,它会掩盖内置函数,如此处所述:Is it safe to use the python word "type" in my code?