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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
