I have been programming into a number of languages like Java, Ruby, Haskell and Python. I have to switch between many languages per day due to different projects I work on. Now, the issue is I often forget to write self
as the first parameter in the function definitions in Python same is with calling methods on the same object.
That said, I am quite amazed by this approach of Python. Basically we have to type more to get the things done, in the languages like Java and Ruby things are made simple by automatically referencing the variables in the current object.
My question is why is this self
necessary? Is it purely a style choice, or is there a reason why Python can't let you omit self
the way Java and C++ let you omit this
?
Best Answer
1) Why is
self
required as an explicit parameter in method signatures?Because methods are functions and
foo.bar(baz)
is just syntactic sugar forbar(foo, baz)
. Classes are just dictionaries where some of the values are functions. (Constructors are also just functions, which is why Python doesn't neednew
) You can say that Python makes it explicit that objects are built from simpler components. This is in accordance with the "explicit is better than implicit"-philosophy.In contrast, in Java objects really are magic and cannot be reduced to simpler components in the language. In Java (at least until Java 8) a function is always a method owned by an object, and this ownership cannot be changed due to the static nature of the language. Therefore there is no ambiguity about what
this
refers to, so it makes sense to have it implicitly defined.JavaScript is an example of a language that has an implicit
this
like Java, but where functions can exist separately from objects like in Python. This leads to a lot of confusion about whatthis
refers to when functions are passed around and called in different contexts. Many instinctively thinkthis
must refer to some intrinsic property of the function, while it is actually purely determined by the way the function is called. I believe havingthis
as an explicit parameter like in Python would make this much less confusing.Some other benefits of the explicit
self
-parameter:Decorators are just functions which wraps other functions. Since methods are just functions, decorators works just as fine on methods. If there were some kind of implicit self, decorators would not work transparently on methods.
Classmethods and static methods does not take an instance parameter. Classmethods take a class as the first argument (typically called
cls
). The explicitself
orcls
parameters makes it much clearer what is going on, and what you have access to in the method.2) Why must instances variables always be qualified with"
self.
?In Java, you don't need to prefix member variables with "
this.
", but in Python "self.
" is always required. The reason is that Python does not have an explicit syntax for declaring variables, so there would be no way to tell ifx = 7
is supposed to declare a new local variable or assign to a member variable. Specifyingself.
solves this ambiguity.