变量默认值的 Python 快捷方式是另一个变量值,如果它是 None [英] Python shortcut for variable default value to be another variable value if it is None

查看:61
本文介绍了变量默认值的 Python 快捷方式是另一个变量值,如果它是 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数声明:

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

是否有 python 快捷方式,其中 type 等于 'name' 值(默认为 r 传递)如果是 None ?

解决方案

通常的成语是:

def set_data(obj=None, name='delp', type=None):如果类型为无:类型 = 名称

(但实际上不要使用 type 作为变量名,它会屏蔽内置函数,如下所述:在我的代码中使用 python 单词type"是否安全?)

I have the following function declaration:

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

Is there a python shortcut, where type is equal with 'name' value(default r passed) if is None ?

解决方案

The usual idiom is:

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

(But don't actually use type as a variable name, it'll mask the built in function as described here: Is it safe to use the python word "type" in my code?)

这篇关于变量默认值的 Python 快捷方式是另一个变量值,如果它是 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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