Getter and Setter in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Example of Setters and Getters 
class Student:
    def setId(self,id):
        self.id=id
    def getId(self):
        return self.id
    
    def setName(self,name):
        self.name=name
    def getName(self):
        return self.name
    
    def setCourse(self,course):
        self.course=course
    def getCourse(self):
        return self.course
    
#Calling 
S=Student()
S.setId(101)
S.setName("Vivek")
#S.setCourse("Mtech")
print(S.getId())
print(S.getName())
#print(S.getCourse())

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *