educative.io

Educative

Solution Review 1: Square Numbers and Return Their Sum - Learn

In the program why is a=self.xself.x why cant we just write xx

Hi @VVSLakshmiKanthety
In python, whenever we define a method inside the class, we pass the self as the first parameter. As we know, that class acts as a blueprint for objects, and this blueprint is used to create multiple objects. To access the parameter of the object, which has called the function inside the class, we use the ‘self’ keyword.
In this specific case, obj1 has called the sum function inside the class to access the parameters of obj1 we should use the self keyword with x. By writing simple x(without self) we won’t be able to access the parameter obj1.
I hope that answers the question.