self represents oneself, self.name='xxx', which means that the name attribute value of this class is 'xxx', def _init_(self):xxxx is a method that will be automatically executed when creating an instance of this class. And def test(self):xxxx means that the methods you can call include self.test(). Do you understand this?
selfrefers to the object youwill reference, which is slightly different during initialization and when calling the method. For example
class A: def __init__(self, name): self.name = name def printname(self): print(self.name) a = A('hello') a.printname()
When initializing the object,
selfrefers to this newly created object, soais assigned toself, thenself.nameis equivalent toa.name, so the objectais created An attributename.When calling a method: selfrefers to the object you want to reference, which is the object you want to act on, that is,a. Soselfis assigned the valuea. Soprint(self.name)is equivalent toprint(a.name).
Books:
Python Learning Manualhas a very detailed explanation.
self represents oneself, self.name='xxx', which means that the name attribute value of this class is 'xxx', def _init_(self):xxxx is a method that will be automatically executed when creating an instance of this class. And def test(self):xxxx means that the methods you can call include self.test(). Do you understand this?
When initializing the object,self
refers to the object youwill reference, which is slightly different during initialization and when calling the method. For exampleself
Books:refers to this newly created object, so
ais assigned to
self, thenself.name
is equivalent to
a.name, so the object
ais created An attribute
name.
When calling a method:self
refers to the object you want to reference, which is the object you want to act on, that is,
a. So
selfis assigned the value
a. Soprint(self.name)
is equivalent to
print(a.name).
Python Learning Manualhas a very detailed explanation.