zaro

What is self in Python?

Published in Python Concepts 3 mins read

In Python, self is a convention used to represent the instance of a class. It acts as a reference to the object itself, allowing you to access and modify its attributes and call its methods within the class definition.

Understanding self in Python

Think of a class as a blueprint for creating objects. When you create an object (an instance of the class), you need a way to refer to that specific object within its own methods. That's where self comes in.

Key aspects of self:

  • Instance Representation: As stated by our reference "SELF represents the instance of class." This is the core function of self; it allows an object to refer to itself.
  • Accessing Attributes: self enables you to access the instance's attributes (data members). For example, self.name would refer to the name attribute of the object.
  • Calling Methods: You can call other methods of the same object using self. For instance, self.calculate_area() would call a method named calculate_area() belonging to that specific instance.
  • Automatic Passing: When you call a method on an object, Python automatically passes the object itself as the first argument, which by convention is named self.
  • Not a Keyword: It is essential to understand that self is just a naming convention, not a keyword. While the name self is strongly encouraged by convention and widely accepted, you could technically use another name, though it's not recommended. However, this will affect readability and is considered poor practice.

Practical Example

Consider the following Python code example:

class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def bark(self):
        print(f"{self.name} says Woof!")

    def display_details(self):
        print(f"Name: {self.name}, Breed: {self.breed}")

# Creating instances of the Dog class
my_dog = Dog("Buddy", "Golden Retriever")
another_dog = Dog("Lucy", "Poodle")

# Calling methods using self implicitly
my_dog.bark()
another_dog.display_details()

In this example:

  • The __init__ method initializes the name and breed attributes of a Dog object.
  • self.name inside the __init__ method refers to the name attribute of the object being created.
  • The bark method prints a message using the object’s name.
  • The display_details method uses self to access the objects attributes.
  • When my_dog.bark() is called, the object my_dog is automatically passed as the self argument.

Summary

Aspect Explanation
Purpose Represents the instance of a class within its own methods.
Function Allows access to attributes, calling methods, and referring to the object.
Passing Python automatically passes the instance of the object when a method is called on that object, using convention self.
Convention Is not a reserved keyword, but is the accepted convention for the first parameter of methods inside a class.

In essence, self is crucial for writing object-oriented code in Python because it enables each instance of a class to maintain its own state and behavior. This is essential for creating unique and independent objects within a class.