0

What is the importance of self keyword in Python?

While defining class, in 'init' function, we have a parameter self. Please explain the importance of self keyword if possible.

14th Apr 2019, 8:42 AM
Arnab Nayak
Arnab Nayak - avatar
1 Resposta
+ 5
self is not a keyword parameter. It is a positional parameter referring to the class instance you want to execute methods with. Since it is a positional parameter you can use any name for it (that isn't used otherwise in Python). Using "self" is just a convention. When calling a method of an instance, the instance itself gets automatically passed as an argument. You don't need to specify it. If what I wrote, doesn't make sense to you, I suggest that you read up on OOP again. Hope that helped.
14th Apr 2019, 9:07 AM
Thoq!
Thoq! - avatar