What is self in Python

What is Self in Python?

In this tutorial, we will learn about what is self in Python. In Python, self is a conventional name that refers to the instance of a class within the class’s methods. It acts as a reference to the current object being accessed or manipulated. When defining methods in a class, the self parameter is used as the first parameter in the method definition.

The purpose of self is to differentiate between instance-specific attributes and local variables within a class. It allows each instance of a class to maintain its own state and access its unique data.

self is used within methods to access and modify instance attributes. For example, if we have an instance variable called name, we can access it using self.name within the class methods. This enables proper encapsulation and interaction with the instance’s specific data.

RELATED POST: Python Timeit Module

How to use Self in Python?

  • Differentiation: self distinguishes between instance variables and local variables within a class, ensuring that each instance can maintain its own state.
  • Attribute access: Python self can be used to access and modify instance attributes within class methods.
  • Instance-specific behavior: Python self allows methods to operate on the specific instance they are called upon, allowing for personalized behavior and data manipulation.

Example:

class Person:
    def __init__(self, name):
        self.name = name
    
    def say_hello(self):
        print("Hello, my name is", self.name)
# Creating an instance of the Person class
person = Person("Arun")
# Calling the say_hello() method on the person instance
person.say_hello()


Output:

Hello, my name is Arun

In this example, self is used within the __init__ method and say_hello method of the Person class.

  • In the __init__ method, self represents the newly created instance of the class. It is used to set the name attribute of the instance. self.name = name assigns the name argument passed to the __init__ method to the name attribute of the instance.
  • In the say_hello method, self refers to the instance on which the method is being called. self.name accesses the name attribute of that particular instance.

Example:

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

    def display_info(self):
        print("Brand:", self.brand)
        print("Color:", self.color)

    def change_color(self, new_color):
        print("Changing color from", self.color, "to", new_color)
        self.color = new_color

# Creating instances of the Car class
car1 = Car("Toyota", "Blue")
car2 = Car("Honda", "Red")

# Displaying initial information
car1.display_info()
car2.display_info()

# Changing the color of car1
car1.change_color("Green")

# Displaying updated information
car1.display_info()
car2.display_info()


Output:

Brand: Toyota
Color: Blue
Brand: Honda
Color: Red
Changing color from Blue to Green
Brand: Toyota
Color: Green
Brand: Honda
Color: Red

Example:

class Calculator:
    def __init__(self, number):
        self.number = number
    
    def square(self):
        result = self.number ** 2
        return result
    
    def cube(self):
        result = self.number ** 3
        return result

# Creating an instance of the Calculator class
calculator = Calculator(5)

# Calculating the square and cube of the number
square_result = calculator.square()
cube_result = calculator.cube()

# Displaying the results
print("Square:", square_result)
print("Cube:", cube_result)


Output:

Square: 25
Cube: 125

In this example, we have a Calculator class with two methods: square and cube. The class takes a number as an input through the __init__ method, which initializes the instance with the given number.

  • The square method calculates the square of the number stored in the instance using the ** operator. The result is stored in the result variable and returned.
  • The cube method calculates the cube of the number stored in the instance using the ** operator. The result is stored in the result variable and returned.

We create an instance of the Calculator class with the number 5. Then, we call square and cube methods on the instance, which perform the respective calculations based on the stored number. The results are stored in square_result and cube_result, respectively.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!