Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get a AttributeError in this code?

I am using python 3.9

I have a class called ImageMetaData

class ImageMetadata(BaseModel):
    title: Optional[str] = None
    source: Optional[str] = None
    sourcesubtype: Optional[str] = None
    filesize: Optional[int] = None
    url: Optional[str] = None

and a function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I get an error about AttributeError:

‘dict’ object has no attribute ‘filesize’

Am I missing something?

>Solution :

"filesize" is a dictionary key, not an attribute. Use i["filesize"], not i.filesize. This is not the same.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading