温馨提示×

python函数如何设置默认值

九三
197
2021-02-18 13:48:44
栏目: 编程语言

python函数如何设置默认值

在python中使用def关键词给函数设置默认值,具体方法如下:

def关键词语法:

def 函数名(...,形参名,形参名=默认值):

代码块

使用方法:

def dis_str(str1,str2 = "http://www.baidu.com/python/"):

print("str1:",str1)

print("str2:",str2)

dis_str("http://www.baidu.com/shell/")

dis_str("http://www.baidu.com/java/","http://www.baidu.com/golang/")

输出结果为:

str1: http://www.baidu.com/shell/

str2: http://www.baidu.com/python/

str1: http://www.baidu.com/java/

str2: http://www.baidu.com/golang/



0