I don't understand 'self' in Python class

34 Views Asked by At

So, what I know about python class is that if I want to assign values to instance attributes, I have to use self, something like this:

class Myclass:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
    def calculation(self):
        return self.num1*self.num2

This is understandable, since I know that java also uses similiar things like 'this' to specfically refer to instance attributes, but what I don't understand here is that even in other functions in the class I have to use self(like calculation() in the code), even if the function doesn't have parameters that have the same name. I feel like there's got to be better way to write instance attributes than this.

Is it true that I have to use self every time I write instance attributes? If not, what are some other ways to write instance attributes?

0

There are 0 best solutions below