Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Object-oriented PythonJuniorVery commonTheory

What is `self`, and why must it be listed explicitly as the first parameter of every instance method?

Answer

`self` is simply the conventional name for the parameter that receives the instance a method is being called on; Python does not use an implicit `this` like some other languages. When you write `obj.method(arg)`, Python translates that into `ClassName.method(obj, arg)` under the hood, so the instance is always passed as the first positional argument, and the method signature must accept it explicitly. `self` is a naming convention, not a keyword — any name would work, but every Python style guide expects `self`.

Strong answer includes

  • self receives the instance the method is called on
  • obj.method(arg) is translated to Class.method(obj, arg)
  • self is convention, not a reserved keyword

Practical example

Python example
instance.greet()

and

Python example
Person.greet(instance)

call exactly the same underlying function with exactly the same arguments; `self` inside `greet` refers to `instance` either way.

#oop#classes

Sources

The Python TutorialPython Software Foundation · Baseline syntax, control flow, functions and module basicsData modelPython Software Foundation · Dunder methods, object identity, attribute lookup and the descriptor and context manager protocolsTop Python Interview Questions and AnswersGeeksforGeeks · Coverage signal for frequently asked fundamentals and OOP questions