码迷,mamicode.com
首页 > 编程语言 > 详细

python中将实参变为可选项

时间:2021-03-10 13:42:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:iss   file   aaa   报错   style   fir   last   red   cal   

 

1、

>>> def a(first,meddle,last):
    b = f"{first} {meddle} {last}"
    return b.title()

>>> a("aaa","bbb","ccc")
Aaa Bbb Ccc
>>> a("aaa","bbb")   ## 少一个实参报错
Traceback (most recent call last):
  File "<pyshell#159>", line 1, in <module>
    a("aaa","bbb")
TypeError: a() missing 1 required positional argument: last
>>> def a(first,last,meddle = ""):    ## 将一个实参设定为空
    b = f"{first} {meddle} {last}"
    return b.title()

>>> a("aaa","bbb","ccc")
Aaa Ccc Bbb
>>> a("aaa","ccc")
Aaa  Ccc
>>> def a(first,last,meddle = None):   ## 讲一个实参设定为None , None等价于 “”。
    b = f"{first} {meddle} {last}"
    return b.title()

>>> a("aaa","ccc","bbb")
Aaa Bbb Ccc
>>> a("aaa","ccc")
Aaa None Ccc

 

>>> def a(first,meddle,last):
    b = f"{first} {meddle} {last}"
    return b.title()

>>> a("aaa","bbb","ccc")
Aaa Bbb Ccc
>>> a("aaa","ccc")      ## 少一个实参,报错
Traceback (most recent call last):
  File "<pyshell#171>", line 1, in <module>
    a("aaa","ccc")
TypeError: a() missing 1 required positional argument: last

 

python中将实参变为可选项

标签:iss   file   aaa   报错   style   fir   last   red   cal   

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14510515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!