2

我是 Python 的初学者,试图理解函数参数及其类型和顺序。

我尝试对不同类型的论点进行一些实验,这是我的实验

def main():
    test_a(2, 33, 44)
    test_b(2, 33)
    test_c(2)
    ## test_d(2,,44) **Produces Invalid syntax**

    test_e(2,33,44,55,66)
    test_f(2, 44,55,66, y = 44)

    test_g(2, 33, 44,55,66, rofa = 777, nard = 888)
    ##test_h(2, 33, foo = 777, boo = 888, 44,55,66)  **Invalid Syntax in Function definition
    ##test_l(2, 44,55,66 ,  foo= 777, boo = 888, y  = 900) **Invalid Syntax in Function definition
    test_m(2, 44,55,66 , y = 900, foo=77777 , boo = 88888)

#############################################################
## NO optional arguments
def test_a(x,y,z):
    print("test_a : x = {}, y = {}, z = {} ".format(x ,y ,z))

## One optional argument at the end
def test_b(x, y, z = 22):
    print("test_b : x = {}, y = {}, z = {} ".format(x ,y ,z))

## TWO optional arguments at the end
def test_c(x, y = 11, z = 22):
    print("test_c : x = {}, y = {}, z = {} ".format(x ,y ,z))

## ONE optional argument at the middle
## Produces Non-default argument follows default argument
#### **** DEFAULT ARGUMENTS MUST COME AT THE END **** ####
## def test_d(x, y = 11, z):
##    print("test_d : x = {}, y = {}, z = {} ".format(x ,y ,z))

#################################################################

## NO optional argument + One List argument
def test_e(x, y, *args):
    print("test_e : x = {}, y = {} ||".format(x, y), end= " ")
    for i in args :
        print(i)

## One optional argument + One list argument
def test_f(x, *args , y = 5):
    print("test_f : x = {}, y = {} ||".format(x, y), end= " ")
    for i in args :
        print(i)

################################################################

## No optional argument, one list, one keyword arguments
def test_g(x,y,*args, **kwargs):
    print(x, y)
    for i in args:
        print(i)

    for i, v in kwargs.items():
        print(i, v)

## **kwargs befor *args produces syntax error !!!
##def test_h(x,y, **kwargs, *args):
##    print(x, y)
##    for i in args:
##        print(i)
##
##    for i, v in kwargs.items():
##        print(i, v)

## **kwargs befor optional argument produces syntax error !!!
##def test_l(x,*args,**kwargs, y = 5):
##    print(x, y)
##    for i in args:
##        print(i)
##
##    for i, v in kwargs.items():
##        print(i, v)
##    

## One optiona, list and keyword arguments
def test_m(x,*args,y = 5, **kwargs):
    print(x, y)
    for i in args:
        print(i)

    for i, v in kwargs.items():
        print(i, v)


if __name__ == "__main__":
    main()

经过这次实验,我真的明白了大部分事情。但是有一个问题我无法进入我的脑海。

在可选参数和列表参数之前定义的函数定义中test_htest_m**kwargs我运行程序时,即使我没有使用该函数,也只是定义它..它产生Syntax Error..我很高兴知道为什么这发生了?

谢谢。

4

2 回答 2

3

关键字参数必须是传递给函数的最终参数。在这里阅读它。像这样的参数**kwargs必须是函数签名中的最后一个参数(如文档中该页面的详细说明)。从文档中:

当存在 **name 形式的最终形式参数时,它会接收一个字典(请参阅映射类型 - dict),其中包含除了与形式参数相对应的关键字参数之外的所有关键字参数。

即使您不使用该函数,您的代码也会引发 SyntaxError 的原因是,如果您违反此规则,Python 甚至无法完成函数定义,因为您尝试给它的签名是非法的。

于 2013-05-09T17:41:34.763 回答
1

*args并且**kwargs必须是指定的最后一个参数(按此顺序!);原因是它们本质上是“包罗万象”,匹配传递给函数的参数,这些参数与预先列出的特定参数不匹配,因此将它们列在特定参数之前是不明确的。

例如,上面的 test_m 必须指定为

def test_m(x, y=5, *args, **kwargs):
    ...
于 2013-05-09T17:40:49.853 回答