Unlike most of the [most popular] object-oriented languages that use a keyword to refer to the object that is "target" of a method call (usually called this
), the Python language requires that every method has an additional parameter (the first, and conventionally called self
) to represent that object. This seems like a case of code boilerplate , which you have to always write but does not add a lot of things ...
(Note: this is a particular case of the use of "pronouns" in the code, something possible in the Lisp language in Lisp and extensively used in the Perl language )
There is some concrete reason for not using an implicit this
... or that a method is transformed into a normal function by a reverse operation:
var foo = {
bar:function(a, b) {
return this.x + a + b;
}
};
var bar = foo.bar;
var foo2 = { x:10 };
bar.call(foo2, 20, 30); // O método original mantém o this explícito,
// mas ainda podemos atribuí-lo explicitamente
So, I see no reason to make explicit the self
, just unnecessary code ... Is there any strong argument in favor, perhaps coming from the author (s) of the Python language itself? (or another language that uses similar strategy)