- *,传递参数是关键字。在*后的参数通过关键字传递参数,一般作为形式参数时,赋予初始值,表示位置参数结束
def safe_division_c(number, divisor, *, numberzero=False, divisorzero=False):
print("===================")
print(numberzero)
print(divisorzero)
if numberzero & divisorzero:
print("除数或者被除数为0了")
else:
return number / divisor
# 连个参数
s = safe_division_c(10, 5, numberzero=True, divisorzero=False)
print(s)
# 一个参数
s = safe_division_c(20, 4, numberzero=True)
print(s)
s = safe_division_c(20, 4, divisorzero=True)
print(s)
# 零个参数
s = safe_division_c(20, 4)
print(s)
- *args:传递参数是元组
# 可变参数之元组
def func_1(*args):
sum = 0
for i in args:
sum += i
return sum
print(func_1(1, 2, 3, 4, 5))
print(func_1(1, 2, 3, 4, 5, 6))
- **kwargs:传递参数是字典
# 可变参数之字典
def func_2(**kwargs):
print(kwargs)
func_2(name="张三", age=18)