Skip to content

Why does the code not work for magicattr_start.py when specifying an else clause for __setattr__  #9

Open
@jeffl24

Description

@jeffl24

Issue Overview

For the file magicattr_start.py, you used the code:
`

def __setattr__(self, name, value):
    if name == 'price':
        if type(value) is not float:
            raise ValueError(
                "The price attribute must be a float data type")
    return super().__setattr__(name, value)

`

I tried to add an else clause right before the final return statement, to make my code more explicit:

`

def __setattr__(self, name, value):
    if name == 'price':
        if type(value) is not float:
            raise ValueError(
                "The price attribute must be a float data type")
    else: 
        return super().__setattr__(name, value) 

`
However this throws an error: "AttributeError: 'Book' object has no attribute 'price'".

I tried searching on stackoverflow and using ChatGPT to find out what is the reason behind this, but to no avail.

Could you please help? Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions