1. The meaning of self in Python
self, the meaning of the English word is obvious, expressing oneself, itself.
There are several potential implications here:
1. The self here refers to the instance instance itself.
2. At the same time, since the word "self" is mentioned in relation to "others"; and the others here refer to the class Class and other variables, such as local variables and global variables wait.
The self here is an object (Object), which is an instance of the current class.
Therefore, the valueName in the corresponding self.valueName and self.function(): represents the self object, that is, the variable of the instance. Corresponding to others, Class variables, global variables, and local variables.
function: Indicates that the self object is called, that is, the function of the instance. It corresponds to other global functions.
2. Why is there a self in Python
That is:
In the code (function) of the class, it is necessary to access the variables and functions in the current instance, that is, to access the Instance:
-
Corresponding variable (property, property): Instance.ProperyNam, to read the previous value and write the new value
-
Call the corresponding function (function): Instance.function(), that is, execute the corresponding action
-> But you need to access the variables of the instance and call the function of the instance, of course, you need the corresponding instance Instance object itself
-> In Python, it is stipulated that the first parameter of the function must be the instance object itself, and it is recommended that, by convention, write its name as self
-> So, we need self (need to use self)
And if self is not used, that is, after removing self in the code, the variables used in that writing method are actually not what you want, not the variables and functions in the real instance, but access to other parts variables and functions too. It can even lead to subsequent inaccessible errors due to not properly initializing instance variables.
Below, the code is used to demonstrate what errors will occur if self is removed or self is not used properly.
3. First look at __init__() and the self object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
可以看出self为实例变量p,是一个Person类型的对象。
1 2 3 4 5 6 7 8 9 10 11 |
|
以下是d = Dog('LiChuang',"京巴")实例化的示意图:
4、如果没有在__init__中初始化对应的实例变量的话,导致后续引用实例变量会出错
如下代码,完整的演示了,如果没有在类Class的最初的__init__函数中,正确的初始化实例变量,则会导致后续没有变量可用,因而出现AttributeError的错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
从上述代码可见,由于在类的初始化(实例化)的__init__函数中,没有给self.name设置值,使得实例中,根本没有name这个变量,导致后续再去访问self.name,就会出现AttributeError的错误了。
对应的,如果写成self.name,则意思就正确了,就是初始化的时候,给实例中新增加,并且正常设置了正确的值newPersionName了,所以后续再去通过self.name,就可以访问到,当前实例中正确的变量name了。
相应的正确写法的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
5、在函数中,使用对应的变量,虽然代码是可以运行的,但是实际上却是使用的,不是实例中的变量
有时候,虽然你写的代码,可以运行,但是使用到的变量,由于没有加self,实际上是用到的不是实例的变量,而是其他的变量。
此类问题,主要和Python中的变量的作用域有关,但是此处例子中,也和是否使用self有关:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
其中,可见,此处开始__init__中,没有给self实例初始化对应的name,
而后面的函数sayYourName中,虽然可以调用到self.name而没有出现AttributeError错误,
但是实际上此处的值,不是所期望的,传入的name,即"Tim",而是类中的name的值,即"class global name"。