Advertisements
Advertisements
प्रश्न
Explain the importance of self in Python classes.
स्पष्ट कीजिए
Advertisements
उत्तर
- self refers to the current instance (object) of a class.
- It is the first parameter of every instance method, including __init__.
- It is used to access or modify instance attributes and call other instance methods.
- Python passes it automatically during a method call.
class Demo:
def show(self):
print("Object method called")
d1 = Demo()
d1.show() # Python internally uses Demo.show(d1)
self is not a reserved keyword, but it is the standard Python convention.
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
