Write a python program to implement Getters and Setters in a class


This Python program defines a Student class that encapsulates student details such as name, total, and percentage. It demonstrates the use of setter and getter methods to set and retrieve these attributes. Here's an explanation of the program:

  • class Student:: This is the class definition for the Student class.
  • def __init__(self):: This is the constructor method for the Student class. It initializes three instance variables: name, total, and per with default values.
  • self.name = "", self.total = "", self.per = 0: These lines initialize the name as an empty string, total as an empty string, and per as 0.
  • def setName(self, name):: This is a setter method for setting the student's name.
  • self.name = name: The name attribute is updated with the value passed to the setName method.
  • def getName(self):: This is a getter method for retrieving the student's name.
  • return self.name: The name attribute is returned when the getName method is called.
  • Similar setter and getter methods are defined for the total and per attributes.
  • name = input("Enter a Name :"): The program prompts the user to enter a name, and the input is stored in the name variable.
  • total = int(input("Enter a Total :")) : The program prompts the user to enter a total, and the input is converted to an integer and stored in the total variable.
  • per = float(input("Enter a Percentage :")): The program prompts the user to enter a percentage, and the input is converted to a float and stored in the per variable.
  • s = Student(): An instance of the Student class is created, representing a student.
  • s.setName(name), s.setTotal(total), s.setPercentage(per): Setter methods are used to set the student's name, total, and percentage based on the user's input.
  • n = s.getName(), t = s.getTotal(), p = s.getPercentage(): Getter methods are used to retrieve the student's name, total, and percentage.
  • print("\nDisplaying Student Details"): A message is printed to indicate that student details will be displayed.
  • print("Name :", n), print("Total :", t), print("Percentage :", p): The student's name, total, and percentage are displayed to the console using the values retrieved from the getter methods.

This program demonstrates the use of object-oriented principles, encapsulation, and the setter and getter methods to manage and retrieve student details. It allows the user to input student information and then displays the details of the student.


Source Code

class Student:
    def __init__(self): #Constructor
        self.name = ""
        self.total = ""
        self.per = 0
 
    def setName(self,name):
        self.name = name
    def getName(self):
        return self.name
 
    def setTotal(self,total):
        self.total = total
    def getTotal(self):
        return self.total
 
    def setPercentage(self,per):
        self.per = per
    def getPercentage(self):
        return self.per
 
# Get user input for name, total, and percentage
name = input("Enter a Name :")
total = int(input("Enter a Total :"))
per = float(input("Enter a Percentage :"))
 
s = Student()
 
#Set the attributes using setter methods
s.setName(name)
s.setTotal(total)
s.setPercentage(per)
 
# Get the attributes using getter methods
n = s.getName()
t = s.getTotal()
p = s.getPercentage()
 
print("\nDisplaying Student Details")
print("Name :", n)
print("Total :", t)
print("Percentage :", p)

Output

Enter a Name :Bob
Enter a Total :450
Enter a Percentage :95.0

Displaying Student Details
Name : Bob
Total : 450
Percentage : 95.0

Example Programs