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

贊助商

分類目錄

贊助商

最新文章

搜索

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

作者:admin    時(shí)間:2022-4-14 17:38:54    瀏覽:

AttributeError: 'dict' object has no attribute 'name',中文意思是:AttributeError: 'dict' 對(duì)象沒(méi)有屬性 'name'。

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

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

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

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

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

返回:

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

我們可以在 Python dict 中可以使用方括號(hào)[],

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

返回:

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

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

let user = {

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

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

    }

print(user.name);

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

 

AttributeError: 'dict' 對(duì)象沒(méi)有屬性 'name'

總結(jié)

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

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

您可能對(duì)以下文章也感興趣

標(biāo)簽: Python  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */