我正在使用 python 3.9
我有一個名為 ImageMetaData 的類
class ImageMetadata(BaseModel):
title: Optional[str] = None
source: Optional[str] = None
sourcesubtype: Optional[str] = None
filesize: Optional[int] = None
url: Optional[str] = None
和一個函式
def total_filesize(images: List[ImageMetadata]):
total = 0
try:
for i in images:
total = total i.filesize
except BaseException as ex:
logger.error("Caught exception trying to compute total filesize for images", exc_info=True)
return total
我收到關于 AttributeError 的錯誤:
‘dict’ object has no attribute ‘filesize’
我錯過了什么嗎?
uj5u.com熱心網友回復:
"filesize"是字典鍵,而不是屬性。使用i["filesize"],不是i.filesize。這不一樣。
uj5u.com熱心網友回復:
你應該修改函式total_filesize。像這樣嘗試:
def total_filesize(images: List[ImageMetadata]):
total = 0
try:
for i in images:
total = total i["filesize"]
except BaseException as ex:
logger.error("Caught exception trying to compute total filesize for images", exc_info=True)
return total
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/388987.html
標籤:蟒蛇-3.x
