python中将实参变为可选项

2021-06-08 21:04

阅读:570

标签: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 "", line 1, in 
    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 "", line 1, in 
    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


评论


亲,登录后才可以留言!