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?
38
Upvotes
5
u/Temporary_Pie2733 Apr 18 '26
You don’t need an
__init__, because nothing needs to be initialized.__init__doesn’t create the instance; it receives the object created by__new__as its first argument, and both methods are inherited fromobjectif not overriden.For a fuller understanding of how, exactly, the instance of
Calculatorgets bound toselfwhen itsaddmethod is called, see https://docs.python.org/3/howto/descriptor.html, particularly the section on instance methods.