-3

How to use "self" keyword regarding variables? It seems that you can set a class variable inside of __init__ constructor by using "self" prefix???

melpomene
  • 84,125
  • 8
  • 85
  • 148
Wade
  • 21
  • 1
  • 2
  • 7

1 Answers1

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