Skip to content

🐍 Python Tutorial: Understanding self, Constructors, and Destructors in Python Classes

In this tutorial, we'll explore the self keyword, the concept of constructors and destructors in Python. These are fundamental concepts in object-oriented programming (OOP) that allow you to create and manage objects effectively.


πŸ“‹ Table of Contents

  1. What is self?
  2. Why Do We Need self?
  3. Using self in Class Methods
  4. Constructors in Python
  5. Destructors in Python
  6. Practical Example: Building a Simple Class
  7. Summary

1. What is self?

In Python, self is a reference to the current instance of the class. It allows you to access the instance’s attributes and methods from within the class.

Key Points:

  • self is not a keyword, but it’s a widely used convention.
  • It’s the first parameter of any method in a class and is used to refer to instance attributes.

2. Why Do We Need self?

The self keyword is essential for several reasons:

  1. Distinguishing Instance Variables: Without self, it would be difficult to differentiate between instance variables (attributes) and local variables inside methods.
class Car:
    def __init__(self, model):
        self.model = model  # 'self.model' refers to the instance attribute
  1. Accessing Instance Attributes and Methods: self allows you to access and modify attributes and methods that belong to the current instance.
class Car:
    def __init__(self, model, speed):
        self.model = model
        self.speed = speed

    def display_info(self):
        print(f"Model: {self.model}, Speed: {self.speed}")
  1. Making the Code Reusable: With self, you can reuse the same method across different instances of the class. The method behaves according to the specific instance it’s called on.
car1 = Car("Toyota", 120)
car2 = Car("Honda", 130)

car1.display_info()  # Outputs: Model: Toyota, Speed: 120
car2.display_info()  # Outputs: Model: Honda, Speed: 130

3. Using self in Class Methods

Every method within a class must include self as its first parameter if it needs to access instance variables. Let’s take a look at a simple class:

class Dog:
    def __init__(self, name, breed):
        self.name = name  # Assign instance variable 'name'
        self.breed = breed  # Assign instance variable 'breed'

    def bark(self):
        print(f"{self.name} is barking!")

my_dog = Dog("Buddy", "Golden Retriever")
my_dog.bark()  # Outputs: Buddy is barking!

In this example:

  • self.name and self.breed are instance variables.
  • The bark() method uses self.name to print a message specific to the object it’s called on.

4. Constructors in Python

What is a Constructor?

A constructor in Python is a special method that is automatically called when a new instance of a class is created. The primary purpose of a constructor is to initialize the instance’s attributes. In Python, the constructor is defined using the __init__ method.

How to Define a Constructor?

The __init__ method is where you define the initial state of an object by setting the values of instance variables.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print(f"Person created: {self.name}, {self.age} years old")

# Creating an instance of Person
person1 = Person("Alice", 30)  # Outputs: Person created: Alice, 30 years old

In this example:

  • The __init__ method is the constructor.
  • When person1 is created, __init__ initializes the name and age attributes and prints a message.

Why Use a Constructor?

  • Automatic Initialization: It ensures that an object is always initialized in a valid state.
  • Code Readability: It makes the creation of objects clearer and more consistent.

5. Destructors in Python

What is a Destructor?

A destructor in Python is a special method that is automatically called when an object is about to be destroyed. The purpose of a destructor is to perform any necessary cleanup before the object is removed from memory. In Python, the destructor is defined using the __del__ method.

How to Define a Destructor?

The __del__ method is where you define any cleanup actions you need to take before an object is destroyed.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print(f"Person created: {self.name}, {self.age} years old")

    def __del__(self):
        print(f"Person deleted: {self.name}")

# Creating and deleting an instance of Person
person2 = Person("Bob", 25)
del person2  # Outputs: Person deleted: Bob

In this example:

  • The __del__ method is the destructor.
  • When person2 is deleted using del, the __del__ method is called, and a message is printed.

Why Use a Destructor?

  • Resource Management: It is useful for releasing resources such as files or network connections that the object might have been using.
  • Automatic Cleanup: It helps ensure that resources are properly released when the object is no longer needed.

6. Practical Example: Building a Simple Class

Let’s build a more comprehensive example that includes a constructor and destructor.

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner  # Instance attribute for account owner
        self.balance = balance  # Instance attribute for balance
        print(f"Account created for {self.owner} with balance {self.balance}")

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited {amount}. New balance is {self.balance}.")

    def withdraw(self, amount):
        if amount > self.balance:
            print("Insufficient funds!")
        else:
            self.balance -= amount
            print(f"Withdrew {amount}. New balance is {self.balance}.")

    def __del__(self):
        print(f"Account for {self.owner} closed. Final balance was {self.balance}")

# Create and delete a bank account
account = BankAccount("Alice", 100)
account.deposit(50)  # Outputs: Deposited 50. New balance is 150.
account.withdraw(75)  # Outputs: Withdrew 75. New balance is 75.
del account  # Outputs: Account for Alice closed. Final balance was 75.

In this example:

  • The __init__ method initializes the owner and balance attributes when the account is created.
  • The __del__ method ensures that a message is printed when the account object is deleted, indicating the final balance.

7. Summary

Concept Explanation
What is self? A reference to the current instance of the class.
Constructor (__init__) Special method to initialize an object’s state.
Destructor (__del__) Special method to clean up before an object is destroyed.
Why use self? To access and manage instance variables and methods.
Automatic Initialization Ensures objects are created with a valid initial state using constructors.
Resource Cleanup Ensures resources are properly released when objects are no longer needed using destructors.