AttributeError: can’t set attribute in Python

AttributeError: can’t set attribute in Python

Python is one of the most widely used programming languages. It might be possible that we could get such type of error while working in Python. If you are facing ‘AttributeError: can’t set attribute in Python’, it means that you are trying to assign a value to a read-only attribute.

In this blog, we will learn multiple scenarios where we can get this type of error. Also, we will learn how to solve this error.

Table of Contents:

What is AttributeError: can’t set attribute in Python?

The error ‘AttributeError: can’t set attribute in Python’ occurs when we try to assign a value to an attribute that cannot be modified, either because it is read-only or does not exist in the object. We get this type of error while working with OOPs concept in Python language.

Python is an object-oriented programming language used to create classes and objects. It also supports encapsulation, abstraction, inheritance, and polymorphism.

There may be multiple reasons for the error ‘AttributeError: can’t set attribute‘ in Python. Here, we will explore the most common causes of this error.

  • Trying to Modify a Read-Only Attribute
  • Assigning to a Property with No Setter Method

1. Trying to Modify a Read-Only Attribute

When we try to modify an attribute that is read-only, we will get the error ‘AttributeError: can’t set attribute in Python’.

Let’s learn this concept with an example:

Code:

class Student:
    def __init__(self):
        self._name = 'Riya'

    @property
    def name(self):  
        return self._name  

student = Student()
print(student.name)  

student.name='Pinki'

Output:

Riya
ERROR!
Traceback (most recent call last):
  File '<main.py>', line 12, in <module>  # trying to modify a read-only attribute
AttributeError: property 'name' of 'Student' object has no setter

Explanation: In the above example, we have created a class Student, which contains an attribute name. In the line student.name = ‘Pinki‘, we are trying to assign a value to the getter function name, which only returns the student’s name and does not allow modification.

2. Assigning to a Property with No Setter Method

When we do not have a setter method to update the value of attribute, we might get this type of error.

Let’s see this with an example:

Code:

class Student:

    def __init__(self, name):

        self._name = name 

    @property

    def name(self): 

        return self._name 

student = Student('Raushani')

print(student.name) 

student.name = 'Priya'

Output:

Raushani
ERROR!
Traceback (most recent call last):
  File '<main.py>', line 12, in <module>
AttributeError: property 'name' of 'Student' object has no setter

Explanation: In the above example, we have not a setter function to update the value of the attribute, Therefore we are getting an error.

Solution for ‘AttributeError: can’t set attribute in Python

Here are the following methods by which we can solve the above error in Python:

1. Trying to Modify a Read-Only Attribute

We can fix the above error by accessing the value directly, without trying to modify the read-only variable.

Code:

class Student:

    def __init__(self):

        self.name = 'Riya'

student= Student();

print(student.name)

Output:

Riya

2. Assigning to a Property with No Setter Method

We can solve the above problem by adding a setter function to modify the value of the attribute.

Code:

class Student:
    def __init__(self, name):
        self._name = name  

    @property
    def name(self):  
        return self._name  

student = Student('Raushani')
print(student.name)  

student._name = 'Priya'
print(student.name) 

Output:

Raushani
Priya

Conclusion

So far in this blog, we have learned why ‘AttributeError: can’t set attribute’ This error occurs in Python. Also, we have learned how we can solve the above error with the help of the above-mentioned methods.

If you want to excel in your career in Python, you may refer to our Python Course.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner