Understanding the self Method in Python: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-28 01:20:32   189  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.

It must be provided as the extra parameter inside the method definition. 

Example:

class Details:
    name = "Simran"
    age = 20

    def desc(self):
        print("My name is", self.name, "and I'm", self.age, "years old.")

obj1 = Details()
obj1.desc()

Output:

My name is Simran and I'm 20 years old.