当前位置: 首页 > 图灵资讯 > 行业资讯> python缺省参数的使用注意

python缺省参数的使用注意

发布时间:2024-07-04 14:54:08

说明

1、定义缺失参数的位置,确保参数列表末尾有默认值的缺失参数。

2、调用函数时,若有多个缺失参数,则需要指定参数名称。

实例

defprint_info(name,title="",gender=True):
"""
:paramtitle:职位
:paramname:班上同学的名字
:paramgender:True男孩False女孩
"""

gender_text="男生"

ifnotgender:
gender_text="女生"

print("%s%s是%s"%(title,name,gender_text))


#提示:在指定缺失参数的默认值时,应使用最常见的值作为默认值!
print_info("小明")
print_info("老王",title="班长")
print_info("小美",gender=False)

以上是使用python缺乏参数的注意事项,希望对大家有所帮助。更多Python学习指导:python基础教程

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12