100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is the self Keyword in Python?

Learn what self means in Python classes — instance binding, self vs cls, and how it compares to this in Java — with examples.

easyQ87 of 226 in Object Oriented Programming Est. time: 4 minsLast updated:
Open Code Lab

Expected Interview Answer

"self" is the conventional name for the first parameter of every instance method in a Python class, and it refers to the specific object the method is being called on.

When you call “obj.method(arg)”, Python automatically passes “obj” as the first argument, bound to the parameter named “self” inside the method definition — you never pass it explicitly at the call site. Through “self”, a method reads and writes that particular instance's attributes (e.g. "self.name = name"), so different objects of the same class keep independent state. "self" is not a reserved keyword; it is a naming convention, and any identifier would technically work, but every Python style guide and codebase uses “self” for readability. Static methods omit it entirely, and class methods use “cls” instead to refer to the class rather than an instance.

  • Lets each instance keep its own independent attribute values
  • Makes instance methods able to read and mutate object state
  • Distinguishes instance methods from static and class methods
  • Keeps method definitions explicit about which object they operate on

AI Mentor Explanation

Every player on the field wears a name tag that lets the umpire say “your strike rate” and have it resolve to that specific player's numbers, not a generic total. "self" is that name tag inside a method: it tells the code exactly which player's (object's) data to read or update. Two players running the same “battingAverage” calculation get two different answers because each carries their own tagged stats. Without that binding, the method would not know whose scoreboard to touch.

Step-by-Step Explanation

  1. Step 1

    Define the method with self first

    Every instance method signature starts with “self” as its first parameter, e.g. "def deposit(self, amount):".

  2. Step 2

    Call the method on an instance

    Writing “account.deposit(50)” is syntactic sugar for "Account.deposit(account, 50)".

  3. Step 3

    Python binds self automatically

    The interpreter passes the instance itself as the first argument — you never supply it explicitly.

  4. Step 4

    Use self to touch instance state

    Inside the method, "self.balance" reads or writes that specific object's attribute.

What Interviewer Expects

  • Correct explanation that self refers to the calling instance
  • Awareness that self is a convention, not a reserved keyword
  • Distinction between instance methods (self), class methods (cls), and static methods (neither)
  • A short code example showing self used to set and read an attribute

Common Mistakes

  • Claiming self is a Python keyword like “this” is a reserved word in Java
  • Forgetting to include self as the first parameter and getting a TypeError
  • Confusing self with cls in classmethod definitions
  • Believing self must literally be named “self” for the code to work

Best Answer (HR Friendly)

In Python, "self" is how an instance method refers back to the specific object it was called on. When you call a method on an object, Python automatically passes that object in as the first argument, and by convention we name that parameter “self.” It is what lets each object keep its own separate data even though every instance shares the same method code.

Code Example

Python self vs Java implicit this (conceptual equivalent)
# Python
class Account:
    def __init__(self, balance):
        self.balance = balance  # self binds to this instance

    def deposit(self, amount):
        self.balance += amount  # updates only this object’s balance

a = Account(100)
b = Account(200)
a.deposit(50)   # a.balance -> 150, b.balance stays 200

// Equivalent Java concept: 'this’ is implicit, never written as a parameter
class Account {
    double balance;
    Account(double balance) { this.balance = balance; }
    void deposit(double amount) { this.balance += amount; }
}

Follow-up Questions

  • Is self a reserved keyword in Python?
  • What is the difference between self and cls?
  • What happens if you forget self in a method definition?
  • How does self compare to this in Java or C++?

MCQ Practice

1. What does “self” refer to inside a Python instance method?

"self" is bound to the instance the method is invoked on, giving access to that object's own attributes.

2. Is “self” a reserved keyword in Python?

Python does not enforce the name “self” — it is convention, though nearly universal in practice.

3. Which parameter does a classmethod use instead of self?

Classmethods receive the class itself, conventionally named “cls”, rather than an instance.

Flash Cards

What is self in one line?The implicit reference to the specific instance an instance method was called on.

Is self a keyword?No — it is a naming convention, not enforced by the language.

How does Python pass self?Automatically, as the first argument, whenever you call obj.method(...).

What replaces self in a classmethod?"cls", which refers to the class rather than an instance.

1 / 4

Continue Learning