首页 > 文章列表 > python函数传递参数的两种方式

python函数传递参数的两种方式

python函数
123 2022-08-07

1、方法说明

向函数传递参数有两种方式,一种是不带关键字的传递,一种是带关键字的传递。

非关键词参数的传递一定要在关键词参数传递之前。

2、实例

*arguments用来接收所有多余的非关键词参数。而**keywords用来接收所有额外的关键词参数。

def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。