技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營

贊助商

分類目錄

贊助商

最新文章

搜索

原因分析:AttributeError: 'dict' object has no attribute 'name'

作者:admin    時間:2022-4-14 17:38:54    瀏覽:

AttributeError: 'dict' object has no attribute 'name',中文意思是:AttributeError: 'dict' 對象沒有屬性 'name'。

這是在Python編程中常常不小心就出現(xiàn)的問題,這是由于對某種語言的編程習(xí)慣造成的,例如本文主題提及的:'dict' 對象沒有屬性 'name',這可能是你使用了錯誤的語法。

原因分析:attributeError: 'dict' object has no attribute 'name'

作為一個之前的 JavaScript 開發(fā)者,我經(jīng)常在 Python 中忽略這條規(guī)則,導(dǎo)致 AttributeError。

在 JavaScript 中,我們可以使用普通對象來存儲鍵值對,然后我們可以使用點(diǎn)運(yùn)算符.或方括號[]來獲取值:

let user = {
      "name" : "卡卡測速網(wǎng)",
      "url" : http://www.howtostagehomes.com/
}
console.log(user.name);
console.log(user["name"]);

返回:

卡卡測速網(wǎng)
卡卡測速網(wǎng)

我們可以在 Python dict 中可以使用方括號[],

let user = {
      "name" : "卡卡測速網(wǎng)",
      "url" : http://www.howtostagehomes.com/
}
print(user["name"]);

返回:

卡卡測速網(wǎng)

但我們使用點(diǎn)運(yùn)算符.訪問時:

let user = {

      "name" : "卡卡測速網(wǎng)",

      "url" : "http://www.howtostagehomes.com/"

    }

print(user.name);

就返回了標(biāo)題寫的錯誤了:AttributeError: 'dict' object has no attribute 'name'

 

AttributeError: 'dict' 對象沒有屬性 'name'

總結(jié)

作為一個之前的 JavaScript 開發(fā)者,我經(jīng)常在 Python 中忽略這條規(guī)則,導(dǎo)致 AttributeError。

我寫這篇文章是為了提醒自己和他人。

您可能對以下文章也感興趣

標(biāo)簽: Python  
x