This article is intended to be as intuitive as possible for those who write "def \ _ \ _ init \ _ \ _ (self):" that appears in Python class definitions as if they were memorized for the time being. I wrote it to do.
Python takes the following format when defining a class.
class demo_class():
def __init__(self):
pass
Since this is not intuitive (for python), I think that there are many beginners other than myself who are struggling.
\ _ \ _ Init \ _ \ _ () is a special name given to the method that initializes an individual object when it is created from the class definition.
Well, I'm not sure if you say that much. However, this introductory Python 3 continued to explain what the Python processing system does, and it was easy to understand, so I will summarize it and explain it with additional figures.
To make it easier to explain, add the member variable name of the class and add the element to add "hoge".
class demo_class():
def __init__(self,name):
self.name = name
a = demo_class("hoge")
print("a: ",a)
print("a.name: ",a.name)
[Output result]
a: <__main__.demo_class object at 0x1023b6668>
a.name: hoge
Python can instantiate a class just like executing a function. An instance is an entity of an object, and instantiation roughly means making a program executable in memory.
In fact, where does the python interpreter run from in the line below?
a = demo_class("hoge")
It will be processed in the above order. It looks like the figure below
First of all, create an empty class object from the class definition
The generated class object has to modify the previous object in order to initialize itself (add "hoge" to the name member). At this time, the python interpreter does not know what kind of initialization should be done to which object, and does not know how to refer to the variable.
The \ _ \ _ init \ _ \ _ method is a special method for initialization.
That is, ** to make it clear which object you want the Python interpreter to initialize and how ** to the special method \ _ \ _ init \ _ \ _ method for initialization ** the empty object you just created Is passed as a self argument **.
This allows you to identify your own objects and eliminates the need to create extra names for each class.
The problem here is "self.name".
def __init__(self,name):
self.name = name
"Self.name" on the left side ** defines a member variable **, so ** create a name variable in the object and assign the name containing the argument "hoge" ** That is. In other words, ** the name on the first line and the name on the right side of the second line represent different things **. Since this is the same place, I think it's hard to understand at first glance.
The reason for the premise of "self." Is that it makes it explicit which object the interpreter should refer to for processing. (maybe)
(In python, anything that follows a "." Is called an attribute.)
In this article, there are many parts that are asserted as "to do XX" or "to do XX" and explained as a means for a limited purpose, but this is to grasp the image. This is the method taken to make it the first goal, and there are some problems in view of the long learning span.
Getting Started Python3 Official documentation
Recommended Posts