Table of Contents
- Introduction to self in Python classes
- Example of self in Python Classes:
- Why Use self?
- Example of Multiple Instances:
- Conclusion of self in Python classes:
Introduction to self in Python classes
Python classes use the self
convention in instance methods to refer to the specific object being manipulated or operated on.
In other words, self
allows you to access the attributes and methods of the current object, and it is used to differentiate between instance variables (attributes) and local variables within methods.
Key Points About self
:
- Refers to the Current Instance:
- The keyword
self
refers to the current instance of a class. In addition, we use it to access the instance’s attribute and methods. Every method in a Python class implicitly receivesself
as its first argument, which is a reference to the object that invoked the method.”
- The keyword
- Implicitly Passed to Instance Methods:
- When you invoke a method on an object, Python automatically passes the
self
argument behind the scenes. Consequently, the first parameter of every instance method must always beself
.
- When you invoke a method on an object, Python automatically passes the
- Not a Keyword:
- While
self
isn’t a Python keyword, it’s a widely adopted convention among Python developers. Although you could technically use alternative names likethis
orobj
, adhering to theself
convention enhances code readability and maintainability.
- While
- Accessing Instance Attributes:
- Leveraging
self
, you can access or modify instance attributes specific to a particular object. For instance,self.attribute_name
refers to the attribute of the current instance.
- Leveraging
Example of self
in Python Classes:
class Dog:
def __init__(self, name, age):
self.name = name # Instance variable 'name'
self.age = age # Instance variable 'age'
def bark(self):
print(f"{self.name} says woof!")
def get_age(self):
return self.age
# Creating an instance of the Dog class
my_dog = Dog("Buddy", 5)
# Calling methods on the instance
my_dog.bark() # Output: Buddy says woof!
print(my_dog.get_age()) # Output: 5
Explanation of self
in the Example:
__init__(self, name, age)
: The__init__
method serves as the constructor for a class. Usingself
, you can assign values to instance variables such asname
andage
for the specific object being created. As a result, each object possesses its own unique set of these instance variables.self.name
andself.age
: These are instance variables, unique to eachDog
object. They store the name and age of the specific dog instance.self.bark()
: Thebark
method usesself
to access thename
attribute of the current object.
Why Use self
?
- Accessing Object Attributes: Without
self
, you wouldn’t be able to access or modify instance variables that belong to the specific object. - Distinguishing Instance Variables from Local Variables: Our declared variables in this methods are confined to the method’s scope.To differentiate between these local variables and the class’s attributes,
self
is employed to access the instance variables. - Enabling Multiple Instances: Each object possesses its own unique set of attributes. Therefore,
self
guarantees that every instance of the class can maintain distinct values for these attributes.
Example of Multiple Instances:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"Car make: {self.make}, Model: {self.model}")
# Creating two instances of the Car class
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
car1.display_info() # Output: Car make: Toyota, Model: Corolla
car2.display_info() # Output: Car make: Honda, Model: Civic
car1
and car2
are two distinct objects, each with its own make
and model
attributes. The self
parameter enables each object to store and access its specific attributes.
Conclusion of self in Python classes:
self
is a reference to the instance of the class. It allows methods to access and modify instance-specific data and differentiate between instance variables and local variables.
It’s a convention that every instance method in a class must include self
as its first parameter.
By using self
, you enable Python’s object-oriented features like encapsulation and allow each instance of a class to have its own state and behavior.
Leave a Reply