首页 > 编程语言 > 详细

python 缺省参数注意事项

时间:2021-06-06 00:37:54      阅读:15      评论:0      收藏:0      [点我收藏+]

必须保证 带有默认值的缺省参数 在参数列表末尾

所以,以下定义是错误的!

def print_info(name, gender=True, title):

 

如果有 多个缺省参数需要指定参数名,这样解释器才能够知道参数的对应关系!

def print_info(name, title="", gender=True):
    """

    :param title: 职位
    :param name: 班上同学的姓名
    :param gender: True 男生 False 女生
    """

    gender_text = "男生"

    if not gender:
        gender_text = "女生"

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


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

 

python 缺省参数注意事项

原文:https://www.cnblogs.com/dazahui/p/14854174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!