I'm just starting out with Python and I'm full of things I don't understand. (I'm sorry if this article is also wrong) It's almost my memorial article.
When studying classes in Python, self comes up. However, it seems like a magic, so I'm not sure, so I will summarize what I investigated.
main.py
class Cat():
def __init__(self,name):
self.name = name
def naku(self):
print("Meow")
tama = Cat("Tama")
tama.naku()
Cat.naku(tama)
In python
main.py
receiver.Method(argument…)
,
main.py
Receiver class.Method(receiver,argument…)
Interpret as. I don't know why. (really sorry) However, it works the same regardless of which one you write, and when you understand self, it is easier to understand the former as the latter, so I will explain it in that way.
For now, let's take a step-by-step look at the entire code. First, there is the Cat class.
main.py
class Cat():
def __init__(self,name):
self.name = name
def naku(self):
print("Meow")
There is a name in the instance variable, The naku function sounds "Nya".
Since a class is like a concept, it is actually summoned (instantiated) with the name "Tama".
main.py
tama = Cat("Tama")
Yes, this has summoned the cat Tama-chan. Next, let me ring.
main.py
#receiver.Method(argument…)
tama.naku() #=>Meow
Yes, it screamed. This is another way of writing, isn't it?
main.py
#Receiver class.Method(receiver,argument…)
Cat.naku(tama) #=>Meow
Yes, what you want to see here is that "tama" is in the receiver part of the argument parentheses. So, if you compare it with the naku function defined in the class again,
python
#neko function definition
def naku(self):
#Function call
Cat.naku(tama)
The arguments matched. This is the true identity of self.
In other words, here it is "tama = self". It is often explained that "self is the instance itself", but simply because the function called "receiver class.method (receiver, argument ...)" is called in the python grammar, the first It seems that it is useless because the number of arguments does not match unless the instance itself is put in one argument (receiver).
The same is true for the init function. Considering the operation of the init function called when summoning Tama-chan,
python
#Receiver class.Method(receiver,argument…)
Cat.__init__(tama,"Tama")
You can understand that the code is done internally.
No, why did the "tama" argument of Cat () become the argument of the init function! ?? That's a question that certainly comes up, but if you think about it carefully, the init function is a special one that is always called at the time of instantiation, unlike the function you define yourself, so it is taken as an argument of Cat (). However, I think that it is automatically included in the argument of the init function. (Please let me know if anyone knows.) Besides, classes are not functions, so they don't need arguments.
--self is just an argument --Internally called by "receiver class.method (receiver, argument)" ――You can manage without worrying about it ――The sentences I write are long! !!
Recommended Posts