Python __init__() and self explained

🚀 Python – Interview Question 📌 Question: What is __init__() in Python and how does self play a role in it? 🔹 What is __init__()? ✔ __init__() is Python’s constructor method in OOP. ✔ It is automatically called when an object is created. ✔ Used to initialize instance variables (attributes). 👉 It runs right after object creation. 🔹 Role of __new__() (Important for Interviews) ✔ __new__() handles memory allocation. ✔ It is called before __init__(). ✔ After memory is created → __init__() initializes the object. 🔹 What is self? ✔ self refers to the current instance of the class. ✔ It allows access to instance variables and methods. ✔ Must be the first parameter in instance methods. 💡 Interview Key Points: ✔ __init__() = Constructor ✔ __new__() = Memory allocation ✔ self = Reference to current object ✔ Automatically called during object creation 👉 Follow Ashok IT School for daily Python interview questions 👉 Comment “PYTHON” for more concepts 👉For Python Course Details  Visit : https://lnkd.in/gf23u2Rh . #Python #OOPS #InitMethod #SelfKeyword #PythonInterviewQuestions #Programming #CodingInterview #AshokIT

  • text

To view or add a comment, sign in

Explore content categories