python - What is the purpose of the `self` parameter? Why is it needed? - Stack Overflow
Python class __init__ and self
When do you use 'self' in Python? - Stack Overflow
Understanding classes, init and self
Videos
I tried learning OOP from many video tutorials and documentation but I'm not understanding why we really need it or what is the use of it.
So it would be really helpful if someone could explain to me like a child what is the need for self and init in python.
Also, If you could tell me how did you fully grasp the concept of OOP in Python that would be really beneficial to me.
Thank You.
The reason you need to use self is because Python does not use special syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically but not received automatically, the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions and leaves the actual name to use up to you (although self is the convention, and people will generally frown at you when you use something else.) self is not special to the code, it's just another object.
Python could have done something else to distinguish normal names from attributes -- special syntax like Ruby has, or requiring declarations like C++ and Java do, or perhaps something yet more different -- but it didn't. Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self.
Let's say you have a class ClassA which contains a method methodA defined as:
class ClassA:
def methodA(self, arg1, arg2):
... # do something
and objectA is an instance of this class.
Now when objectA.methodA(arg1, arg2) is called, python internally converts it for you as:
ClassA.methodA(objectA, arg1, arg2)
The self variable refers to the object itself.
Adding an answer because Oskarbi's isn't explicit.
You use self when:
- Defining an instance method. It is passed automatically as the first parameter when you call a method on an instance, and it is the instance on which the method was called.
- Referencing a class or instance attribute from inside an instance method. Use it when you want to call a method or access a name (variable) on the instance the method was called on, from inside that method.
You don't use self when
- You call an instance method normally. Using Oskarbi's example, if you do
instance = MyClass(), you callMyClass.my_methodasinstance.my_method(some_var)not asinstance.my_method(self, some_var). - You reference a class attribute from outside an instance method but inside the class definition.
- You're inside a staticmethod.
These don'ts are just examples of when not to use self. The dos are when you should use it.
Use self to refer to instance variables and methods from other instance methods. Also put self as the first parameter in the definition of instance methods.
An example:
class MyClass(object):
my_var = None
def my_method(self, my_var):
self.my_var = my_var
self.my_other_method()
def my_other_method(self):
# do something...