What is the use of self in Python?
self is the first parameter of a method in a class. It refers to the current object, so the method can reach the attributes and other methods of that object. Python passes it automatically when you call the method.
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hi, I am", self.name)
s = Student("Robin")
s.greet()
Output:
Tutorials Class - Output Window
Hi, I am Robin
self is not a keyword. You could use another name, but everyone uses self, so please do the same. Learn more in Python Classes and Objects.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |