Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 18

Explain the use of self in a class.

Answer:

In Python, self is a conventional name used as the first parameter in instance methods of a class. It represents the instance of the class and allows access to the attributes and methods of the class in Python. Here’s a detailed explanation of the use of self in a class:

Key Points About self

  1. Instance Reference:

    • self refers to the instance of the class. When an instance method is called, self points to the particular instance that invoked the method.
  2. Accessing Instance Variables:

    • self is used to access instance variables and methods within the class. This allows each instance of the class to have its own unique set of attributes.
  3. Distinguishing Between Class and Instance Variables:

    • Using self helps distinguish between instance variables (unique to each instance) and class variables (shared among all instances).
  4. Convention, Not a Keyword:

    • self is not a reserved keyword in Python. It is just a strong convention. You can use any name instead of self, but it is highly recommended to stick with self to maintain code readability and consistency.

Example: Using self in a Class

Here is a simple example to illustrate the use of self:

class Dog:
    def __init__(self, name, age):
        self.name = name  # Instance variable
        self.age = age    # Instance variable
    
    def bark(self):
        print(f"{self.name} is barking.")
    
    def get_age(self):
        return self.age

# Creating instances of Dog class
dog1 = Dog("Buddy", 5)
dog2 = Dog("Molly", 3)

# Accessing instance methods and variables
dog1.bark()  # Output: Buddy is barking.
print(dog1.get_age())  # Output: 5

dog2.bark()  # Output: Molly is barking.
print(dog2.get_age())  # Output: 3

Detailed Explanation:

  1. Constructor (__init__ method):

    • The __init__ method is a special method that initializes a new instance of the class. It takes self as the first parameter, followed by any additional parameters required for initialization.
    • In the example, self.name and self.age are instance variables initialized in the __init__ method.
  2. Instance Methods:

    • The bark method and get_age method are instance methods that also take self as their first parameter.
    • self.name and self.age are used within these methods to access the instance variables of the specific object that called the method.
  3. Creating Instances:

    • When dog1 = Dog("Buddy", 5) is executed, a new instance of the Dog class is created, and self within the __init__ method refers to dog1.
    • Similarly, for dog2 = Dog("Molly", 3), self refers to dog2.

Summary

  • self is a reference to the current instance of the class and is used to access variables that belong to the instance.
  • Instance Variables: Variables that are unique to each instance of the class.
  • Instance Methods: Functions that operate on instances of the class and typically use self to access instance variables and other methods.
  • Convention: self is a strong convention but not a keyword, promoting readability and consistency in class definitions.

Using self correctly is essential for working with classes in Python, ensuring that each instance can maintain its own state and behavior independently of other instances.

Recent job openings