簡體   English   中英

SyntaxError:位置參數跟隨關鍵字參數

[英]SyntaxError: positional argument follows keyword argument

我想知道下面的說法是否正確。

SyntaxError:位置參數跟隨關鍵字參數

我知道什么是位置參數和關鍵字參數,但對上述語句感到困惑。 例如當我們說

A 跟隨 B 這意味着

//這意味着“A”在“B”之后

因此,以同樣的方式,當我們調用任何 function 時,我們應該先傳遞位置參數,然后傳遞關鍵字參數。 在這種情況下,正確的陳述應該是

***SyntaxError: keyword argument follows positional argument***

例子:

    def test(a,b,c):
        print(f"Sum of all no is : {a+b+c}")
    test(a=10,20,c=30)
    test(a=10,20,c=30)
//output                   ^
SyntaxError: positional argument follows keyword argument

示例2:

Passing positional argument first.
    def test(a,b,c):
        print(f"Sum of all no is : {a+b+c}")
    test(20,c=10,b=30)
    //output
    Sum of all no is : 60

這是 Python 語法的一個特點。 它按照它們首先出現的順序解釋位置 arguments,然后是關鍵字 arguments 作為下一個。 例如這段代碼:

def test(a,b,c):
    print(f"Sum of all no is : {a+b+c}")
test(10,20,a=30)

返回“TypeError: test() got multiple values for argument 'a'”。 因為 Python 在位置上將 10 和 20 解釋為前兩個變量,所以 'a' 和 'b' 已經使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM