python学习之函数一

传递任意数量的实参

python函数中在预先不确定需要多少参数的时候,可以再形参前面加*号,不管调用语句传入多少值,形参都会将它们收入囊中。

def make_pizza(*toppings):
    print(toppings)
make_pizza('mushrooms','green peppers','extra cheese')

形参名*toppings中的号,让python创建一个名为toppings的空元组,并将收到的所有值都封装到这个元组中。

猜你喜欢

转载自blog.csdn.net/Dylan88/article/details/82703172