Easy Programming & OOP Python Programming
What is the purpose of 'self' in Python?
Answer
self is a reference to the current instance of the class, used to access instance attributes and methods. It must be the first parameter of instance methods (though not passed when calling). self.attribute creates/accesses instance variables; without self, it's a local variable. Unlike Java/C++ where 'this' is implicit, Python requires explicit self. Convention is to use 'self' though any name works. Class methods use 'cls' instead of 'self'. self enables each object to maintain its own state.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Python Developer Backend Developer