Easy👤 0-2 years👤 3-5 years 1 min read

What are __init__ and self in a Python class?

Asked inInfosysTCSAccentureDeloitte
#oop#class#init#self#instance
Report issue

⚡ Short Answer

`__init__` is the initializer that runs when you create an object — it sets up instance attributes. `self` is the reference to the current instance, passed automatically as the first parameter of every instance method, so methods can read/write that object's own data.

Coffee Chat Question

Concept Made Simple

What are __init__ and self in a Python class?

🧠Mind Map Answer

Remember It Faster

classblueprint for objects
__init__runs on creation, sets attributes
selfthe current instance
instance vs class attrper-object vs shared

⌨️Hands-on Keyboard

Learn by Doing

python
class Account:
    def __init__(self, owner, balance=0):
        self.owner = owner        # instance attribute
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self.balance

a = Account("Guru", 100)
print(a.deposit(50))
Output
150

🔥What If?

Think Beyond the Expected

What happens if you forget `self` as the first method parameter?

Calling the method raises a TypeError about arguments, because Python passes the instance as the first positional argument automatically — the method needs a parameter (conventionally `self`) to receive it. Without it, the counts don't match. `self` isn't a keyword, just a strong convention.

😂Real World

Classes model real entities — User, Order, Account — bundling their data and behavior; `__init__` + `self` are the first things every Python developer uses when structuring an app's domain objects.

🎯Interviewer's Expectation

Keywords they're listening for:

__init__ initializesself = current instanceinstance attributes via selfauto-passed first argclass vs instance attribute

⚠️Common Mistakes

  • Forgetting self in method signatures
  • Using a mutable class attribute as if it were per-instance
  • Doing heavy work in __init__

Best Practices

  • Set all instance state in __init__
  • Use self for per-object data; class attrs for shared constants
  • Add __repr__ for debuggable objects

🔁Follow-up Questions

  • 1Instance vs class attribute — where does each live?
  • 2What is __str__ / __repr__ for?
  • 3What are @classmethod and @staticmethod?

🧩Related Technologies

dataclasses__repr__classmethod/staticmethod

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: OOP (Python)
Interview question: "What are __init__ and self in a Python class?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions