What does self mean in a class implementation?

Questions & AnswersCategory: PythonWhat does self mean in a class implementation?
Lokesh Kumar Staff asked 2 years ago

What does self mean in a class implementation?

a. The word self represents a class. If we create a method bound to the whole class, we pass self as the first argument to the class method.

b. The word self is a Python keyword.

c. The word self represents an instance of the class. Using the self keyword we can access class attributes and methods in Python. The name self is just a convention. When implementing an instance method, self must be passed as the first argument of the method.

1 Answers
Lokesh Kumar Staff answered 2 years ago

a. The word self represents a class. If we create a method bound to the whole class, we pass self as the first argument to the class method.
Explanation:

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.