1) Why is self
required as an explicit parameter in method signatures?
Because methods are functions and foo.bar(baz)
is just syntactic sugar for bar(foo, baz)
. Classes are just dictionaries where some of the values are functions. (Constructors are also just functions, which is why Python doesn't need new
) 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 what this
refers to when functions are passed around and called in different contexts. Many instinctively think this
must refer to some intrinsic property of the function, while it is actually purely determined by the way the function is called. I believe having this
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 explicit self
or cls
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 if x = 7
is supposed to declare a new local variable or assign to a member variable. Specifying self.
solves this ambiguity.
@staticmethod
before method declaration, suppresses the error(just for info and also not recommended)