pyhton Learning and Data Mining Principle and Application Analysis of self

  • 2021-12-13 08:41:04
  • OfStack

Directory 1. What is class, instance, and object? 2. What is method and what is function? 3. Summary of key SELF analysis

Yes, you are right. This is my soul question when I first learned python.

We always see self in class, but it seems that it is useless, just put it there to occupy a seat.

If you have the same question, congratulations, you didn't learn class.

Therefore, before explaining who self is, let's clarify a few questions:

What is class and what is instance? What is object? What is method and what is function?

1 voice-over, I personally is opposed to programming, originally English proper nouns for Chinese translation. As the saying goes, language shapes thinking, so no matter how well you translate some proper nouns, there will always be a certain degree of semantic ambiguity. For example, "class", when we see the word, we will think of "class", but it is translated as "class", so we will unconsciously understand this proper noun according to "class" or "one major class". This is a very serious potential misleading, because the proper term class has little to do with "class" or "one major category". Therefore, it is better not to translate, just as you don't know what the word means, and then slowly understand the meaning it represents in the process of learning.

In fact, because of the limitations of English, the exclusive nouns in many programming languages are also blinded by bosses. . For example, I spit out axes in matplotlib countless times. . .

Too much talk again. . . Get back to business.

1. What is class, what is instance, and what is object?

Class: It can be understood as an assembly plant. If we want to produce a robot, we should set up a factory first. Make sure first: We need to install the arm first, then the head, and the assembly line of our little broken robot will be set up. This factory is intelligent, and the number of arms and heads can be adjusted.


class BuildRobot():
    def __init__(self,armcount,headcount):
        self.armcount = armcount
        self.headcount = headcount

So class here is a factory called BuildRobot. '__init__' tells the assembly line that first you need the robot to have a few arms ('armcount') and a few heads ('headcount'). Ignore self here for a moment and talk about it later.

At this time, you can play run1, so that your class will be set up. But the factory at this time, because you didn't start production, didn't produce anything. The following is instance

instance: It can be understood as starting the robot produced by the factory once. Now we use the factory we built before to produce a normal 1 o'clock robot with two arms and one head:


normal_robot = BuildRobot(2,1)

Check whether the number of arms under 1 is correct.

Let's have another abnormal robot:


weird_robot = BuildRobot(4,1)

normal_robot and weird_robot are both instance. Although they have different arms, they are essentially robots made of this class, which are composed of arms and heads.

object: This is more troublesome. In most cases, object and instance have the same meaning, which refers to the robot made by this. The difference between the two is only in the English language environment:

normal_robot is an instance of 'buildrobot'

normal_robot is a 'buildrobot' object

The above two statements are equivalent.

2. What is method and what is function?

Both are defined by def. A slightly rough understanding is that function in class is called method. Therefore, method is a kind of function related to class and instance.

Take chestnuts:

Or the above factory, we are now installing a workshop to color the arms:


class BuildRobot():
    def __init__(self,armcount,headcount):
        self.armcount = armcount
        self.headcount = headcount
    def paintarm(self,color):
        print("paint arm:",color)

This paintarm is an method. It's still the same. Now this class is not produced, so there is no actual product for this method. We can only produce one instance:


colorful_robot = BuildRobot(2,1)

OK, we now have an robot with two arms and one head. At this time, our robot is still not colored, because we didn't let this instance enter the colored workshop. Now let's put this robot into the workshop and paint it red.


colorful_robot.paintarm('red')

paint arm: red

The above process is call, paintarm method. Several points:

If you don't build a robot first, this workshop can't color the arm, so to color, you must build a robot first. Therefore, method depends on instance.

This workshop can only color the arms of robot produced by this factory. If you take a car from another factory and let him color it, he won't do it. Therefore, method is dependent on class. Only instance created by this class can call this method.

If I outsource the job of coloring. I just built another factory outside, specializing in coloring, and this is function:


def outsourcing_paint(robot,color):
    print("paint",robot,"arm:",color)

outsourcing_paint(colorful_robot,'red')

paint < __main__.BuildRobot object at 0x116b434a8 > arm: red

This outsourced coloring factory, no matter which factory you come from, whether you are a robot or a robot dog, I will bring it anyway and color my arm.

Seeing this, you should understand the difference between function and method.

Note that there are actually two kinds of method, one is instance method, and the other is class method.

instance method is equivalent to a workshop for various modifications to robots. I color robots, which doesn't affect the shape of my factory, does it? class method is a workshop that modifies the properties of this factory, this class. For example, I have a workshop that is responsible for painting the factory red. This behavior does not affect the size and color attributes of the robot I built.

The discussion in this article is limited to instance method.

3. Focus on SELF analysis

After explaining class and method clearly, we can finally speak self. From the above example, we noticed that


outsourcing_paint(colorful_robot,'red')
In function, there is no self. Because we told the outsourcing factory who to color. So when we defined the outsourcing factory function, we had two input variables: robot and color.

colorful_robot.paintarm('red')
However, when Goose used method, we only told the workshop that I wanted red. Then how does this workshop know which robot to color? Is it for normal robot or for colorful robot? Because we used the format colorful_robot. paintarm () in call, method, paintarm, method, oh, I'm going to color this colorful_robot. In python, in order to make instance. method () work normally, when writing method in class, the first place of the variable must be reserved to refer to the future call, the instance of method. This is equivalent to setting up this workshop for coloring arms, and we must reserve an entrance to put in the robots that have already been produced. The reserved seat can be called by any name. Just for the sake of code beauty, most people choose to use self to refer to instance himself who uses this method.

Summarize

self is a placeholder in the first place of the variable name when writing instance method for class. In writing instance method, the variable self may not be used. If you want to change the attribute of instance in method, you can use self. xxxx to refer to this attribute for modification.

Therefore, self refers to instance made from this class.

The above is pyhton learning and data mining self principle and application analysis details, more information about pyhton data mining self analysis please pay attention to other related articles on this site!


Related articles: