How to use "self" keyword regarding variables? It seems that you can set a class variable inside of __init__
constructor by using "self" prefix???
Asked
Active
Viewed 398 times
-3
-
Please share the relevant code. – kenorb Oct 06 '17 at 14:18
-
6`self` is technically not a keyword. – jwodder Oct 06 '17 at 14:20
1 Answers
1
self
is just a name used as a convention to refer to the instance on which methods are bound. Bound methods are always called with the instance as first argument, and you can name that variable anything.
By using self
in an instance method, we set instance variables and not class ones. Different programming languages provide mechanisms to access the instance some use implicit this
objects, some implicitly call all methods on the instance, and Python explicitly uses passes the instance as the first variable.

hspandher
- 15,934
- 2
- 32
- 45
-
While that answer is technically correct it's not a good answer. It lacks some explanation, for example what are bound methods, or that `self` isn't a keyword. I haven't downvoted though. – MSeifert Oct 06 '17 at 14:22
-
@MSeifert I have mentioned that part, that self is just a conventional name given to the first argument of a bound method on an instance. – hspandher Oct 06 '17 at 14:23