In python class will be used to "self", this article is mainly to talk about the relationship between self and variables in the class.
First look at the sample code I.
class Test(object): def __init__ (self, val1): self.val0 = val1 def fun1(self): print(self.val0) def fun2(self, val2): print(val2) def fun3(self): print(self.fun1) self.fun1() ins=Test(123) ins.new_val=”I'm a new value”
1. what is self
In python classes self represents the instance itself, specifically, the memory address of the instance.
When calling a method of an instance, the Python interpreter itself passes the instance variable to self in the function of the class.
Take the above code I as an example
Code I defines a class Test, in which self is a parameter variable, and when the class Test is instantiated to get instance ins, the python interpreter automatically calls __init__ to execute Test.init(ins, 123), which self can receive the memory address of the instance ins, and thus self represents the
instance itself. Similarly, if you execute ins.fun1( ) after instantiating ins, the python interpreter will interpret ins.fun1( ) as Test.fun1(ins). As you can see, the variable self doesn't require the user to manually pass a value; the interpreter automatically passes an instance to it for us.
Note that self is not a keyword; in other words, it can be replaced with another legal variable name, but the specification and standards recommend that we use self consistently.
2、selfscenarios
There are 3 scenarios for the use of self in classes as follows
1) self is the first argument of a function in a class, e.g. in class, def fun1(self, ...).
It was said above that "when calling a method of an instance, the Python interpreter itself passes the instance variable to self in the function of the class", and if the first parameter of the function of the class is not self representing the instance, the method of the instance is called with no parameter to receive the instance variable that is automatically passed by the interpreter, and thus the The program throws an exception.
In fact, "the only difference between a function defined in a class compared to a normal function is that the first parameter is always the instance variable self and, when called, you don't have to pass that parameter. Other than that, the methods of a class are no different from ordinary functions, so you can still use default parameters, variable parameters, keyword parameters, and named keyword parameters" (from Mr. Xuefeng Liao).
2) In the class, reference the attributes of the instance, example: self.variable name (e.g. self.val0).
The purpose of referencing an instance's properties is to bind properties to, write to, or read from the instance.
For example, in code I, in the function __init__ of the class, "self.val1 = val1" binds the attribute val0 to the instance self (which represents the instance ins after the class is instantiated into ins) and assigns the value of the variable val1 to the instance's attribute val0.
In the function fun1, print(self.val0), the value val0 of the instance self is read and printed, but of course, it is possible to modify the value of the attribute val0 in the function.
3) In the class, call the instance's method, e.g., self.fun1(); get the address of the instance's method, e.g., self.fun1.
Classes are abstract templates, and instances are concrete "objects" created from classes, each with the same methods, but with different data. Since self represents an instance, the method address of the instance can be represented as "self.function name", and the method of the instance can be called as "self.function name()". This can be done in the definition of the class, as well as in calls to instance methods after instantiation.
3. python's several kinds of variables - by scope
1, global variables: in the module, outside all functions, outside the class, this is the global variables.
2, local variables: in the function, in the class of the method (not modified by self), this is the local variable
3, static variables (you can also say, class attributes): in the class, but not in the class of the method, this is the static variable
4, instance variables (can also be said, instance attributes): in the class of the method of the variable, modified with self, this is the instance variable
Note that other categories can be obtained based on other categorization methods, such as private variables.
4. The relationship between self and variables
Combining points 1, 2 and 3 above, you can get the relationship between self and variables in a class. In a nutshell, a variable modified by self is an instance variable, and a variable not modified by self is not an instance variable.
What is the purpose of instance variables, or when is it better to use self to modify a variable? My summary is as follows:
When we want to bind a particular variable to an instance, we use self to modify that variable, in the class. Generally, after the class is instantiated into different instances, a variable is bound to an instance in order for the different instances to not interfere with each other.
Specific scenarios of use
1) If you need to call the same variable in different methods of a class, and the variable does not affect each other in different instances belonging to the same class (i.e., exclude class attributes), bind the variable to an instance in the class.
2) If you need to modify, or refer to a variable of an instance after the class instantiation gets the instance, you bind the variable to the instance in the class.
5. A little supplementation
There are two ways to bind properties to an instance
1) In the function __init__ of the class, bind variables for the instance.
This is also the scenario discussed in this article.
According to pep8, all variables that have self added to them need to make sure that it is the first occurrence in __init__, although there are some scenarios where the program won't be wrong if you don't do this, but follow the rules.
2) Bind new properties to the instance after instantiation. For example, instance variable . Attribute variable = value
Since Python is a dynamic language, thus, instances created based on classes can bind properties arbitrarily. Binding attributes to instances through instance variables is one of the ways to do this, for example, the attribute newval is bound to the instance by the code, ="I'm a new value" in Code I. However, this is not the concern of this discussion.
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.