LeetCampus
Interview Question

What is __init__() in Python and how does self play a role in it?

July 24, 2025
0 views
Difficulty: Easy
Popularity: Common
Share on

Question Explanation

__init__() is a special method in Python that acts as a constructor for a class. It is automatically called when a new instance of the class is created, allowing you to initialize the attributes of the object. Interviewers ask about __init__() and the role of self to assess a candidate's understanding of object-oriented programming principles in Python. Knowing how to properly use constructors is essential for creating well-structured and maintainable code. This topic connects to broader concepts such as class design, encapsulation, and inheritance. Common misconceptions include confusing self with the class name or forgetting to include it as the first parameter in instance methods. Understanding these concepts is crucial because they underpin how Python manages memory and object lifecycle, which is fundamental for any Python developer. Moreover, this knowledge is applicable in real-world scenarios where proper object initialization leads to better performance and reliability of applications. In summary, __init__() and self are foundational elements in Python that every programmer must grasp to excel in software development.

Sample Answers

Example 1: Basic Understanding of init()

__init__() is a method that initializes an object's attributes when it is created. For example, when defining a class Car, you can use __init__() to set the initial properties like color and model:

class Car:
    def __init__(self, color, model):
        self.color = color
        self.model = model

In this case, self refers to the instance of the class being created. When you create a new Car object, you can specify its attributes:

my_car = Car('red', 'Toyota')
print(my_car.color)  # Output: red

This demonstrates how __init__() allows for customized object creation, ensuring each instance has its own unique state.

Example 2: Role of self in Object Initialization

The self keyword is crucial in Python classes as it allows instance methods to access attributes and methods of the class. In the context of __init__(), it helps maintain the state of an object. Consider this example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f'Hello, my name is {self.name} and I am {self.age} years old.'

Here, self.name and self.age are instance variables that store the properties of each Person object. When you instantiate Person:

john = Person('John', 30)
print(john.greet())  # Output: Hello, my name is John and I am 30 years old.

This shows how self is essential for accessing instance-specific data, enabling personalized behavior.

Example 3: Common Pitfalls with init() and self

A common pitfall when using __init__() is forgetting to include self as the first parameter in method definitions. This can lead to unexpected errors. For instance:

class Animal:
    def __init__(name, species):  # Missing self
        self.name = name
        self.species = species

This will raise a TypeError because self is not recognized. Always ensure to include it:

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

Furthermore, understanding that self refers to the instance itself is key to avoiding confusion. If you mistakenly try to use the class name instead of self, it could lead to errors. Thus, mastering the usage of __init__() and self is fundamental for effective Python programming.

Keywords

PythonObject-Oriented ProgrammingConstructorsSelfClasses

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions