Home >Backend Development >Python Tutorial >Let's talk about Self in Python, who is it?
When everyone learns object-oriented Python, they will always encounter an existence that is difficult to understand: self.
Who is this self? Why does every class instance method have a parameter self? What role does it play?
"Let us draw the conclusion first: after the class is instantiated, self is Represents the instance (object) itself."
The simplest way to understand self is that you think of self as "the ID card of the instance (object)."
Python classes It cannot be used directly. It can only function by creating an instance (object). Each instance (object) is unique and it can call methods and properties of the class. Classes are like soul possession, allowing instances (objects) to have their own (self) functions.
Beginners will find that there is a fixed parameter self in class methods (constructor methods and instance methods). In fact, this parameter represents the instance (object) itself. Just like an ID card, the instance can use the ID card to call class methods.
Analogy to human beings, human beings are a Python class, each individual person represents an instance (object), and each person’s ID card represents self in Python, each person You can use your ID card to go to college, take the high-speed rail, stay in a hotel... (method), and instances (objects) in Python can also use self to call class methods.
The above uses an analogy to explain the meaning of self. In the final analysis, self represents the instance itself. "When an instance (object) calls a class method, The object will automatically pass its own reference to the method as the first parameter, and the first parameter is self."
And self is just a conventional way of writing, you can use any other name to replace self. It won't change the meaning of the code, but we generally don't do that.
In order to better illustrate the role of self, let’s take an example. There is a Students class below:
class Students: # 构造方法 def __init__(self,name): self.name = name # 实例方法 def study(self,examination_results): self.examination_results = examination_results print("同学{}的考试分数是{}".format(self.name,self.examination_results)) print("该实例对象的地址是{}".format(self))
Let’s take an example student_a first.
studend_a = Students('studend_a') print(studend_a.name)
The result prints out: student_a.
Let’s take another example student_b.
studend_b = Students('studend_b') print(studend_b.name)
The result is printed out: student_b.
It can be seen that the instances (objects) are different and the printed results are also different. When the class is instantiated, self.name is actually equal to the instance (object).name.
Let’s take the code just now as an example. Let’s call the instance method inside, and self will be printed out, which will make it more obvious.
Instance student_a:
studend_a = Students('studend_a') print(studend_a.study(80))
Output result:
The test score of student student_a is 80. The address of this instance object is .
Instance student_b:
studend_b = Students('studend_b') print(studend_b.study(80))
Output result:
The exam score of student student_b is 80. The address of this instance object is .
You can clearly see that the self printed by the two instances is different, because self represents the instance (object) itself.
Taking the instance student_b as an example, when printing self, the following object information appears.
<__main__.students object at>.
If you print student_b again, the same result will appear.
print(student_b)
Output:
<__main__.students object at>.
Is it clear at this time that after the class is instantiated, self represents the instance (object) itself.
The above is the detailed content of Let's talk about Self in Python, who is it?. For more information, please follow other related articles on the PHP Chinese website!