Read Only Properties in Python

Prerequisites: Python Classes and Objects

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. To put it in simple words, let us assume a class Student, a student can have many properties such as Name, Course, Student’s ID, etc. Now let us assume we have a Student named Anita pursuing MBA, Anita is an object of class Student.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class student:
      
    def __init__(self, name, course):
        self.name = name
        self.course = course
  
    def studentid(self):
        return "student's identification number is \
        {}{}".format(self.name, self.course)
  
student1 = student("Anita", "MBA")
print(student1.studentid())

chevron_right


Output:

student's identification number is AnitaMBA

Now, We want to access student id as a property and not as a method. For this all required is to add @property decorator before the method.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class student:
      
    def __init__(self, name, course):
        self.name = name
        self.course = course
  
    @property
    def studentid(self):
        return "student's identification number is \
        {}{}".format(self.name, self.course)
  
student1 = student("Anita", "MBA")
print(student1.studentid)

chevron_right


Output:

student's identification number is AnitaMBA

A Read-Only-Property is a property decorator without a setter. The student id here is a read-only-property since it doesn’t have a setter. In general terms, it means that the value is not changeable. To understand let’s take one more example:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class employee:
    def __init__(self, basesalary, yearsofworking):
        self.basesalary = basesalary
        self.yearsofworking = yearsofworking
      
    @property
    def salary(self):
        self.salary = 50000
  
amit = employee(20000, 5)
amit.salary = 10000
print(amit.basesalary, amit.yearsworking, amit.salary)

chevron_right


Output:

Traceback (most recent call last):
File “/home/e029e0b9ccad85905e22dd5a91943897.py”, line 14, in
amit.salary = 10000
AttributeError: can’t set attribute

To fix this one setter is to be added in this code. After doing this, it will no longer be a read-only-property.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

class employee:
    def __init__(self, basesalary, yearsofworking):
        self.basesalary = basesalary
        self.yearsofworking = yearsofworking
        self._salary = 0
      
    @property
    def salary(self):
        return self._salary
  
    @salary.setter
    def salary(self, salary):
        self._salary = salary
  
amit = employee(20000, 5)
amit.salary = 10000
print(amit.basesalary, amit.yearsofworking, amit.salary)

chevron_right


Output:

20000 5 10000

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.