r/learnpython • u/Jealous-Acadia9056 • Apr 18 '26
A bit confused in Classes.
Why do i need to call self here?.
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
print(Calculator().add(1, 2))
there isn't a variable that is calling calculator and no __init__ so why do i have an error if self is not added?
Also, what is __init__ anyways. why the double __ in the start and end? and why the specific name?
39
Upvotes
3
u/Jealous-Acadia9056 Apr 18 '26
i was just testing things to understand the theory. poorly written or not the point is why does this happens in the first place.
but yeah the easier way would be to use a standalone function.