What is “self” in Python?

Let me explain what “self” means in Python in a simple way!

When we write code in Python, sometimes we want to create something called an “object”
An object is a special kind of thing that has certain properties (like a color, a size, a shape) and certain things it can do (like make a sound, move around)

To make an object in Python, we write something called a “class”. A class is like a blueprint for an object. It tells Python what properties the object should have and what things it can do

Now, sometimes we want to tell Python to do something with an object that we’ve created from a class. For example, we might want to tell the object to make a sound, or to change its color

When we do this, we use the word “self” to tell Python which object we want to do something with. It’s like saying “Hey Python, I want you to do this thing with THIS object right here!”

So, “self” is just a way for us to tell Python which object we’re talking about when we do something with it. It’s like a special code word that we use to refer to the object that we want to work with

I hope that helps make sense of what “self” means in Python!

Consider the following Python class that represents a car:

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def get_make(self):
return self.make

def get_model(self):
return self.model

def get_year(self):
return self.year

In this example, the “init” method takes in three parameters and initializes the “make”, “model”, and “year” attributes of the instance. The “get_make”, “get_model”, and “get_year” methods return the values of those attributes for the instance they are called on, using the “self” parameter to refer to the instance-specific attributes.

When an instance of the “Car” class is created, the “self” parameter in the methods will automatically refer to that instance.

For example:

my_car = Car("Toyota", "Corolla", 2022)
print(my_car.get_make()) # Output: "Toyota"
print(my_car.get_model()) # Output: "Corolla"
print(my_car.get_year()) # Output: 2022

In this example, when the “get_make” method is called on the “my_car” instance, the “self” parameter refers to the “my_car” instance, allowing access to the “make” attribute specific to that instance.

Here’s another example that might help illustrate the use of “self” in Python classes:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def say_hello(self):
print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")

In this example, the “init” method takes in two parameters and initializes the “name” and “age” attributes of the instance. The “say_hello” method prints out a message that includes the “name” and “age” attributes of the instance.

When an instance of the “Person” class is created, the “self” parameter in the methods will automatically refer to that instance. For example:

person1 = Person("Alice", 25)
person1.say_hello() # Output: "Hello, my name is Alice and I am 25 years old."

In this example, when the “say_hello” method is called on the “person1” instance, the “self” parameter refers to the “person1” instance, allowing access to the “name” and “age” attributes specific to that instance.

I hope these examples help you understand the use of the “self” parameter in Python classes!

5