![]() |
VOOZH | about |
In Python, when defining methods inside a class, first parameter is always self. It is not a keyword, but a naming convention that plays a key role in Python’s object-oriented programming. The self parameter represents instance of the class itself, allowing you to access and modify its attributes and methods.
Code shows how self is used to store data in an object and access it through methods.
('Toyota', 'Corolla')
Explanation:
Now, Let's understand why python uses self.
The main reason Python uses self is to make object-oriented programming explicit rather than implicit. By requiring the instance of the class as the first parameter, Python ensures:
Unlike some other programming languages, Python doesn’t hide this reference automatically. This makes it clear and unambiguous that the method is operating on an instance of the class, which improves readability and avoids confusion (especially in inheritance).
In this example, self is used to initialize an object’s topic and access it inside a method.
Topic: Python
Explanation:
Below example shows how self is used to access attributes of the correct instance while performing calculations.
Area of the circle: 78.5
Explanation: