self — A convention in Python

NamyaLG
Mar 5, 2023

On digging deeper, I discovered that using selfin Python is merely a convention. More often than not, classes in Python utilize self to initialize and refer to attributes and invoke methods.

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

def car_details(self):
print("Model is {}, and color is {}".format(self.model, self.color))

car = Car(color="red", model="Suzuki")
car.car_details()

But it turns out that self is not a keyword or reserved word, but rather a position dedicated to pass a reference to the current instance

Python confirms that self is not a keyword
# works perfectly fine !

class Car:
def __init__(not_self, color, model):
not_self.color = color
not_self.model = model

def car_details(not_self):
print("Model is {}, and color is {}".format(not_self.model, not_self.color))

car = Car(color="red", model="Suzuki")
car.car_details()

The use of self makes it much more intuitive, so let’s keep it that way!

--

--

NamyaLG

Tech-enthusiast | Runner_for_life | #NoHumanIsLimited