Python Interview Questions
Python
Web DevelopmentFrontendBackendData ScienceQuestion 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
-
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.
-
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.
-
Distinguishing Between Class and Instance Variables:
- Using
self
helps distinguish between instance variables (unique to each instance) and class variables (shared among all instances).
- Using
-
Convention, Not a Keyword:
self
is not a reserved keyword in Python. It is just a strong convention. You can use any name instead ofself
, but it is highly recommended to stick withself
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:
-
Constructor (
__init__
method):- The
__init__
method is a special method that initializes a new instance of the class. It takesself
as the first parameter, followed by any additional parameters required for initialization. - In the example,
self.name
andself.age
are instance variables initialized in the__init__
method.
- The
-
Instance Methods:
- The
bark
method andget_age
method are instance methods that also takeself
as their first parameter. self.name
andself.age
are used within these methods to access the instance variables of the specific object that called the method.
- The
-
Creating Instances:
- When
dog1 = Dog("Buddy", 5)
is executed, a new instance of theDog
class is created, andself
within the__init__
method refers todog1
. - Similarly, for
dog2 = Dog("Molly", 3)
,self
refers todog2
.
- When
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.