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.