Python基礎——positional argument vs keyword argument
python強大的型別推導,有時也會帶來一些副作用,比如有時編譯器會報如下錯誤:
TypeError: Function takes at most 1 positional arguments (2 given)
# 函式最多接受一個位置引數,卻提供了兩個
所謂positional argument位置引數,是指用相對位置指代引數。關鍵字引數(keyword argument),見名知意使用關鍵字指代引數。位置引數或者按順序傳遞引數,或者使用名字,自然使用名字時,對順序沒有要求。
A positional argument is a name that is not followed by an equal assign(=) and default value.
A keyword argument is followed by an equal sign and an expression that gives its default value.
以上的兩條引用是針對函式的定義(definition of the function)來說的,與函式的呼叫(calls to the function),也即在函式的呼叫端,既可以使用位置標識引數,也可使用關鍵字。
def foo(x, y):
return x*(x+y)
print(foo(1, 2)) # 3, 使用positional argument
print(foo(y=2, x=1)) # 3,named argument
一個更完備的例子如下:
def fn(a, b, c=1):
return a*b+c
print(fn(1, 2)) # 3, positional(a, b) and default(c)
print(fn(1, 2, 3)) # 5, positional(a, b)
print(fn(c=5, b=2, a=2)) # 9, named(b=2, a=2)
print(fn(c=5, 1, 2)) # syntax error
print(fn(b=2, a=2)) # 5, named(b=2, a=2) and default
print(fn(5, c=2, b=1)) # 7, positional(a), named(b).
print(fn(8, b=0)) # 1, positional(a), named(b), default(c=1)
相關文章
- Python成功解決TypeError: __init__() missing 1 required positional argument: ‘comment‘PythonErrorUI
- 【已解決】TypeError: __init__() takes 1 positional argument but 2 were givenError
- HTTPSConnection.__init__() got an unexpected keyword argument check_hostnameHTTPGo
- Tensorflow Keras load_model報錯got an unexpected keyword argument ‘ragged‘KerasGo
- python argument 1 must be 2-item sequence, not intPython
- javaScript argument 學習筆記JavaScript筆記
- 9,6 argument應用
- OSError: [Errno 22] Invalid argumentError
- 關於argument變數的理解變數
- Methods with Variable Argument Lists (var-args)
- error: invalid type argument of unary ‘*‘ (have ‘int‘) *__first = __tmp;Error
- TypeError: The ‘compilation‘ argument must be an instance of Compilation 報錯Error
- struts:實現圖片的上傳 argument type mismatch errorError
- error creating overlay mount to invalid argument unbuntu系統Error
- FatalThrowableError in index.php line 554:Type error: Argument 1 passedErrorIndexPHP
- -bash: /bin/rm: Argument list too long的解決辦法
- GRE考試中的Argument寫作與實戰攻略
- 刪除大量檔案Argument list too long錯誤解決
- CentOS8 AnolisOS8 yum安裝 No match for argument: htop ErrorCentOSError
- EBS Form不能登入: Illegal argument for colorScheme applet parameterORMSchemeAPP
- Android程式設計權威指南 - 第10章 使用fragment argumentAndroid程式設計Fragment
- jni編譯non-numeric second argument to `wordlist' function錯誤編譯Function
- error: "Invalid argument" setting key "net.ipv4.ip_local_port_range"Error
- AttributeError: module...ops‘ has no attribute ‘_TensorLike‘, ValueError: `updates` argument..eagerError
- 新增檔案到Sdcard出現Failed to push selection: Invalid argument問題AI
- Django一對多關係模型Add a related_name argument to the definit的錯誤Django模型
- python 讀取 csv 檔案報錯:file = builtins.open (filename, mode, buffering),OSError: [Errno 22] Invalid argumentPythonUIError
- npm報錯 TypeError [ERR_INVALID_ARG_TYPE]: The “path“ argument must be of type string.Received undefineNPMError
- 【問題處理】diagcollection.pl採集指令碼執行出錯——Argument list too longGC指令碼
- Python基礎篇-Python基礎01Python
- 從Function.length 與 Argument.length 區別談到如何傳遞任意個數引數Function
- C++基礎:: struct vs classC++Struct
- python基礎中的基礎Python
- zblog升級報錯“Invalid argument supplied for foreach”或者“unserialize(): Error at offset”的解決辦法Error
- Python基礎筆記01-Python基礎Python筆記
- python 基礎Python
- Python 基礎 (-)Python
- python基礎①Python