簡體   English   中英

類型錯誤:get() 不接受關鍵字參數

[英]TypeError: get() takes no keyword arguments

我是 Python 新手,我正在嘗試制作一個哈希表,用於檢查鍵是否指向表中的值,如果沒有,則將其初始化為空數組。 我的代碼有問題的部分是這行:

converted_comments[submission.id] = converted_comments.get(submission.id, default=0)

我收到錯誤:

TypeError: get() takes no keyword arguments

但是在文檔(以及各種示例代碼)中,我可以看到它確實采用了默認參數:

https://docs.python.org/2/library/stdtypes.html#dict.get http://www.tutorialspoint.com/python/dictionary_get.htm

以下是 get() 方法的語法:

dict.get(key, 默認=無)

The Stack 上沒有這方面的內容,所以我認為這是初學者的錯誤?

由於 Python C 級 API 的開發方式,許多內置函數和方法實際上並沒有為其參數命名。 即使文檔調用參數default ,該函數也不會將名稱default識別為引用可選的第二個參數。 您必須按位置提供參數:

>>> d = {1: 2}
>>> d.get(0, default=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: get() takes no keyword arguments
>>> d.get(0, 0)
0

錯誤消息說get接受關鍵字參數,但您提供的參數為default=0

converted_comments[submission.id] = converted_comments.get(submission.id, 0)

許多文檔和教程,例如https://www.tutorialspoint.com/python/dictionary_get.htm ,錯誤地將語法指定為

dict.get(key, default = None)

代替

dict.get(key, default)

暫無
暫無

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

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