Back to Python
2026-01-035 min read

Python self Parameter

Learn Python self Parameter step by step with clear examples and exercises.

Why This Matters

Understanding the self parameter is crucial for mastering object-oriented programming (OOP) in Python. It allows you to create flexible and modular code by enabling methods to manipulate data specific to each object. This knowledge is essential not only for acing coding interviews but also for debugging real-world issues and creating well-structured code.

Prerequisites

To follow this tutorial, you should have a basic understanding of the following concepts:

  • Python programming language syntax
  • Object-oriented programming (OOP) principles
  • Classes, objects, and instance variables in Python

Importance of OOP and self parameter

Object-oriented programming is a powerful paradigm that promotes code reusability, modularity, and maintainability. The self parameter plays a vital role in enabling methods to manipulate data specific to each object, making it easier to write reusable and modular code.

Core Concept

In Python, the self parameter is an implicit first argument passed to every method within a class. It refers to the object that the method belongs to, providing direct access to its instance variables. The self parameter enables methods to manipulate data specific to each object, making it easier to write reusable and modular code.

Here's a simple example demonstrating how to use the self parameter:

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

def display_info(self):
print(f"Brand: {self.brand}")
print(f"Model: {self.model}")

def set_brand(self, new_brand):
self.brand = new_brand

def set_model(self, new_model):
self.model = new_model

my_car = Car("Toyota", "Corolla") # Creating an instance of the Car class
my_car.display_info() # Calling the display_info method on the instance
my_car.set_brand("Honda") # Setting the brand using the set_brand method on the instance
my_car.display_info() # Calling the display_info method again to show updated information

In the above example, we define a Car class with an initializer (__init__) method and methods for setting the brand and model (set_brand and set_model, respectively). The self parameter is used to access instance variables. We create an instance of the Car class called my_car, display its information, set a new brand using the set_brand method, and then display the updated information.

How self works internally

When a method is called on an object in Python, the interpreter automatically passes the object as the first argument (self) to that method. This hidden parameter allows methods to access instance variables directly without having to pass them explicitly as arguments every time.

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

def display_info(self):
print("Brand:", self.brand)
print("Model:", self.model)

my_car = Car("Toyota", "Corolla") # Creating an instance of the Car class
my_car.display_info() # Calling the display_info method on the instance

In the above example, when we call my_car.display_info(), Python automatically passes the my_car object as the self parameter to the display_info method. Inside the method, we can access instance variables using the self parameter (e.g., self.brand).

Worked Example

In this example, we will create a simple bank account class with methods for depositing and withdrawing money. We'll use the self parameter to manage each account's balance.

class BankAccount:
def __init__(self, balance=0):
self.balance = balance

def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"Deposited {amount}. New balance: {self.balance}")
else:
print("Invalid deposit amount.")

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

def check_minimum_balance(self, minimum_balance):
if self.balance >= minimum_balance:
print("Account in good standing")
else:
print("Account needs more funds.")

my_account = BankAccount(100) # Creating an instance of the BankAccount class
my_account.deposit(50) # Depositing money using the deposit method on the instance
my_account.withdraw(75) # Withdrawing money using the withdraw method on the instance
my_account.check_minimum_balance(200) # Checking minimum balance using the check_minimum_balance method on the instance

In the above example, we define a BankAccount class with an initializer method that sets the account's starting balance. We then create a my_account object and perform a deposit, withdrawal, and check the minimum balance using the deposit, withdraw, and check_minimum_balance methods, respectively. The self parameter is used to manage each account's balance within these methods.

Common Mistakes

  1. Forgetting the self parameter in method definitions:
class Car:
def display_info(brand, model): # Missing self parameter
print(f"Brand: {brand}")
print(f"Model: {model}")

To correct this mistake, add the self parameter to the method definition:

class Car:
def display_info(self, brand, model):
print(f"Brand: {brand}")
print(f"Model: {model}")
  1. Accessing instance variables without using self:
class Car:
def __init__(self, brand, model):
brand = brand # Accessing instance variable incorrectly
model = model

To correct this mistake, use the self parameter to access instance variables:

class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
  1. Calling a method without an instance (static methods): If you want to call a method without creating an instance of the class, you can declare it as a static method by adding the @staticmethod decorator and remove the self parameter from the method definition:
class Car:
@staticmethod
def display_info(brand, model):
print(f"Brand: {brand}")
print(f"Model: {model}")

Car.display_info("Toyota", "Corolla") # Calling the static method without creating an instance

Practice Questions

  1. Write a class for a Rectangle with length and width as instance variables. Create methods to calculate the area and perimeter of the rectangle. Use the self parameter to access instance variables within these methods.
  1. Modify the BankAccount example to add a method that checks if the account balance is greater than or equal to a specified minimum balance. If it is, print "Account in good standing"; otherwise, print "Account needs more funds."
  1. Write a class for a Person with name and age as instance variables. Create methods to set and display the person's information. Also, create a static method that calculates the current year based on the person's birth year (assuming the current year is 2022).

FAQ

Question: Can I pass the self parameter explicitly when calling a method?

Answer: No, you don't need to pass the self parameter explicitly when calling a method on an object because Python does it automatically. However, you can use the super() function to access methods from the parent class when the self parameter is not defined in the child class.

Question: What happens if I define a method without the self parameter?

Answer: If you define a method without the self parameter, it becomes a static method and can't access instance variables directly. To make it an instance method that uses the self parameter, add self as the first argument in the method definition.

Question: How do I create a class with only static methods?

Answer: To create a class with only static methods, you can define all its methods using the @staticmethod decorator and omit the self parameter from their definitions:

class Utilities:
@staticmethod
def add(a, b):
return a + b

@staticmethod
def subtract(a, b):
return a - b

In this example, the Utilities class contains only static methods that don't require an instance to be called.

Python self Parameter | Python | XQA Learn