Only one convention to make explicit the object instance within the class.
The author of the language define (in question 4.21) self
as follows:
Self is merely a Conventional name for the first argument of a
method -- i.e. a Function defined Inside a class Definition. A method
defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for
some instance x of the class in which the Definition occurs;
the called method will think it is called as meth(x, a, b, c).
I will create a fictional story that is just a joke to illustrate and try to give an understanding that makes more sense than just a convention as explained by the author.
Suppose I want to write several common functions for a certain object, which in the case of Python can be a simple dictionary.
Thus, when calling each function I wrote, I would have to pass to this function this object and then the parameters of the function itself. Example:
item = {'qt': 10, 'nome': 'Banana', 'preco': 12.20}
def Desconto(objeto, percentual):
objeto['preco'] = objeto['preco'] * (percentual / 100.0)
And to call the function:
Desconto(item, 10)
Let’s say that I assemble a number of functions that work on top of the same type of object. We can say that I created a class functions. For organisational purposes we will agree that the name of the functions shall become NomeDaClasse.NomeDaFunção(Objeto, Param1, Param2, ...)
.
I’d have something like:
def MinhaClasse.Funcao1(Objeto, Param1, Param2, ...)
...
def MinhaClasse.Funcao2(Objeto, Param1, Param2, ...)
...
def MinhaClasse.Funcao3(Objeto, Param1, Param2, ...)
...
Now, whenever I want to call a function just identify the class and know that the first parameter is always an object of a certain type that that class of functions understands.
If I agree that in my source code I can write this set of functions in a different way, for example, not to repeat the class name in the function name, I will put it like this:
class NomeDaClasse ()
def Funcao1(Objeto, Param1, Param2, ...)
...
def Funcao2(Objeto, Param1, Param2, ...)
...
When I wanted to call the function of this class, I would continue to do:
NomeDaClasse.NomeDaFuncao(Objeto, Param1, Param2, ...)
But how I created a very similar way to the class structure of OO, then I could find a way to create a way to pass the attributes of my object on an initializer, which I will also agree with a very different name, for example, __init__
and through it I pass the attribute values of my "object" and create a single instance of that class. Everything will look very much like the classes and objects of OO.
I will also agree that I can call the functions of my object with a simpler syntax, for example:
MeuObjeto.MinhaFuncao(Param1, Param2, ...)
Since I simplified everything but I don’t want the programmer to know that there are things implicit in these conventions of mine, I will keep in the signature of the functions within the class a special name, which I will call self
to specify the parameter that is the object of my class. When calling, internally my compiler would do the following:
MeuObjeto.MinhaFuncao(...) => MinhaClasse.MinhaFuncao(MeuObjeto, ...)
So I made it easy for the programmer who can use a syntax very similar to the syntax of OO and I’ll make it explicit to those conventions I’ve made, leaving the name self
within the signature of class methods.
Only yesterday this was answered: http://answall.com/questions/176250/qual-a-forma-correta-de-chamar-m%C3%A9todos-em-python/176252#176252 , Wallace’s answer explains this
– Miguel
Take a look at Pedro Werneck’s website
– Not The Real Hemingway
@Miguel Thanks for the reminder. Although Wallace’s answer was correct, I thought it was kind of superficial. I would like to understand the real need, the reason besides saying that it belongs to the method. Since in other languages there is no such need.
– Wilker
@Nottherealhemingway thanks, it seems to be a great reference. I will read it calmly soon. If ngm has replied I will try to draw up a reply based on what I have learned and post here.
– Wilker
If it helps, put your conclusions about your question here as an answer and mark your considerations as an answer, please. This will help those who have the same question and the question will be answered. In addition we will have the beginning of a documentation for the problem (I think that’s the goal).
– Not The Real Hemingway
Related: 'Is there an advantage to an explicit "self" instead of the implicit "this"?'
– mgibsonbr