What do __init__ and self do in Python?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Understanding __init__ and self in Python

In Python's object-oriented programming (OOP), __init__ and self are fundamental concepts that facilitate the creation and management of objects. Grasping their roles is essential for writing effective and maintainable Python code. Let's explore what each of these does, why they're important, and how to use them properly.

What is __init__?

Definition

__init__ is a special method in Python known as the initializer or constructor. It is automatically called when a new instance of a class is created. The primary purpose of __init__ is to initialize the object's attributes with values provided during instantiation.

Syntax

class ClassName: def __init__(self, parameters): # Initialization code self.attribute = value

Example

class Person: def __init__(self, name, age): self.name = name # Initializing the 'name' attribute self.age = age # Initializing the 'age' attribute # Creating an instance of Person person1 = Person("Alice", 30) print(person1.name) # Output: Alice print(person1.age) # Output: 30

Explanation:

  1. Class Definition: The Person class has an __init__ method that takes name and age as parameters.
  2. Attribute Initialization: Inside __init__, self.name and self.age are set to the values passed during object creation.
  3. Object Creation: When person1 = Person("Alice", 30) is executed, Python calls the __init__ method to initialize person1's attributes.

What is self?

Definition

self is a conventionally named parameter in Python that refers to the instance of the class. It allows access to the attributes and methods of the class in Python. While self is not a reserved keyword, it is strongly recommended to use self to maintain readability and consistency.

Role of self

  1. Accessing Instance Attributes: self is used to access or modify the attributes of the class instance.
  2. Calling Other Methods: It allows methods within the class to call other methods or access attributes.
  3. Distinguishing Between Instance and Local Variables: Helps differentiate between instance attributes and local variables within methods.

Example

class Circle: def __init__(self, radius): self.radius = radius # Using self to set the instance attribute def area(self): return 3.14 * (self.radius ** 2) # Accessing the instance attribute using self def resize(self, new_radius): self.radius = new_radius # Modifying the instance attribute using self # Creating an instance of Circle circle1 = Circle(5) print(circle1.area()) # Output: 78.5 circle1.resize(10) print(circle1.area()) # Output: 314.0

Explanation:

  1. Initialization: The __init__ method initializes the radius attribute using self.radius = radius.
  2. Accessing Attributes: The area method calculates the area by accessing self.radius.
  3. Modifying Attributes: The resize method changes the radius attribute using self.radius = new_radius.

Why Are __init__ and self Needed?

  1. Object Initialization: __init__ allows you to set up the initial state of an object by initializing its attributes.
  2. Instance-Specific Data: self ensures that each object maintains its own state, allowing multiple instances of a class to have different attribute values.
  3. Encapsulation: Together, they promote encapsulation by bundling data (attributes) and methods that operate on the data within the class.

Common Misconceptions

  1. self is Optional:

    • Misconception: Some beginners think self is optional.
    • Reality: In instance methods, self must be explicitly defined as the first parameter to access instance attributes and methods.
  2. __init__ is the Same as a Constructor:

    • Misconception: __init__ is often confused with the actual constructor.
    • Reality: The actual constructor is __new__, which creates a new instance. __init__ initializes the instance after it's been created.
  3. self is a Keyword:

    • Misconception: self is a reserved keyword in Python.
    • Reality: self is just a naming convention. You can name it differently, but it's highly discouraged for the sake of readability and consistency.

Best Practices

  1. Always Use self as the First Parameter:

    • Stick to the convention of naming the first parameter self to make your code more understandable to others.
  2. Initialize All Necessary Attributes in __init__:

    • Ensure that all essential attributes are initialized within the __init__ method to prevent unexpected behaviors.
  3. Keep __init__ Focused:

    • Avoid putting complex logic in __init__. Instead, delegate complex operations to other methods to keep initialization straightforward.
  4. Use self to Access Attributes and Methods:

    • Always use self when referring to instance attributes and methods within class methods.

Advanced Example

class BankAccount: def __init__(self, account_holder, balance=0): self.account_holder = account_holder # Instance attribute self.balance = balance # Instance attribute def deposit(self, amount): if amount > 0: self.balance += amount print(f"Deposited ${amount}. New balance: ${self.balance}") else: print("Deposit amount must be positive.") def withdraw(self, amount): if 0 < amount <= self.balance: self.balance -= amount print(f"Withdrew ${amount}. New balance: ${self.balance}") else: print("Insufficient funds or invalid amount.") def display_balance(self): print(f"{self.account_holder}'s balance: ${self.balance}") # Creating instances account1 = BankAccount("Alice", 100) account2 = BankAccount("Bob") # Using methods account1.deposit(50) # Output: Deposited $50. New balance: $150 account1.withdraw(30) # Output: Withdrew $30. New balance: $120 account1.display_balance() # Output: Alice's balance: $120 account2.deposit(200) # Output: Deposited $200. New balance: $200 account2.withdraw(250) # Output: Insufficient funds or invalid amount. account2.display_balance() # Output: Bob's balance: $200

Explanation:

  1. Initialization:

    • account1 is initialized with a balance of $100.
    • account2 is initialized with the default balance of $0.
  2. Method Usage:

    • deposit and withdraw methods modify the balance attribute using self.balance.
    • display_balance accesses the account_holder and balance attributes using self.
  3. Instance Independence:

    • account1 and account2 maintain separate states. Operations on one do not affect the other.

Summary

  • __init__:

    • A special method called automatically when a new object is created.
    • Initializes the object's attributes.
    • Acts as the initializer, setting up the object's initial state.
  • self:

    • A reference to the instance of the class.
    • Used to access and modify the object's attributes and methods.
    • Must be the first parameter in instance methods to maintain clarity and consistency.

Understanding __init__ and self is crucial for effective object-oriented programming in Python. They work together to create well-structured, maintainable, and efficient classes by ensuring that each object manages its own state and behavior properly.

Happy Coding!

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to prepare for a project manager interview?
How to understand distributed computing concepts for interviews?
Deliberate practice routines tailored for backend interview prep
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.