In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the principle of pyhton data mining self". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the principle of pyhton data mining self"!
1. What is class, what is instance, what is object?
Class: can be understood as an assembly plant. If we are going to produce a robot, we have to build a factory first. First make sure: we have to install the arm first, and then the head, and the assembly line of our little broken robot will be set up. This factory is relatively 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 the class here is to build a factory called BuildRobot. _ _ init__' tells the pipeline, first of all, how many arms ('armcount') and how many heads (' headcount') you need the robot to have. Ignore the self here first, and we'll talk about it later.
You can run at this time, so that your class is set up. But at this time the factory, because you did not start production, there is no output. Here's instance.
Instance: it can be understood as starting a robot produced in a factory. Now we use the factory we built before to produce a more normal robot, two arms and one head:
Normal_robot = BuildRobot (2jue 1)
Check to see if the arm count is correct?
Let's have another abnormal robot:
Weird_robot = BuildRobot (4jue 1)
Normal_robot and weird_robot are both instance. Although they have different numbers of arms, they are essentially robots made of this class, made up of arms and heads.
Object: this is a bit troublesome. In most cases, object and instance have the same meaning. They both refer to the robot created by this. The difference between the two is only the difference in the English language context:
Normal_robot is an instance of 'buildrobot'
Normal_robot is a 'buildrobot' object
The above two statements are equivalent.
two。 What is method and what is function?
Both are defined by def, and a slightly rougher understanding is that the function in class is called method. Therefore, method is a kind of function related to class,instance.
Take a chestnut:
Or the factory above, we now install an additional workshop, which is responsible for coloring the arms:
Class BuildRobot (): def _ _ init__ (self,armcount,headcount): self.armcount = armcount self.headcount = headcount def paintarm (self,color): print ("paintarm:", color)
This paintarm is a method. Again, there is no production of this class, so there is no actual product of this method. We can only produce one instance first:
Colorful_robot = BuildRobot (2jue 1)
Okay, now we have a robot with two arms and one head. At this time, our robot is still not colored, because we did not let this instance into the workshop where it was painted. Now let's get this robot into the workshop and paint it red.
Colorful_robot.paintarm ('red')
Paint arm: red
The above process is the paintarm method of call. A few points:
If you don't build a robot first, there will be no way to color the arms in this workshop, so if you want to do so, you must first build a robot. Therefore, method is dependent 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 the instance created by this class can call the method.
If I outsource the coloring job. I just set up 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 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 and color my arms.
When you see here, 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 the equivalent of a workshop that makes various modifications to the robot product. I color the robot, it doesn't affect the appearance of my factory, right?
Class method is the workshop that modifies the properties of the factory, the class. For example, I have a workshop that is responsible for painting the factory red. This behavior does not affect the size and color attribute of the robot I built.
The discussion in this article will be limited to instance method.
3. Key SELF analysis
After explaining class and method clearly, we can finally talk about self. Through the example above, we notice that
Outsourcing_paint (colorful_robot,'red')
In function, there is no self. Because we told the outsourced factory who to color. So when defining the outsourcing factory function, we have two input variables:robot and color.
Colorful_robot.paintarm ('red')
However, when the goose was using 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 use the format colorful_robot.paintarm () when we use the method of call, so the method of paintarm knows, oh, I'm going to color this colorful_robot.
In python, for the instance.method () format to work properly, when you write method in class, you must set aside the first bit of the variable to refer to the instance of the future call method. It is equivalent to that when we build this workshop that colors our arms, we have to reserve an entrance to put in the robots that have already been produced.
The seat reserved can be called by any name. Just for the sake of code elegance, most people choose to use self to refer to instance himself who uses this method.
At this point, I believe you have a deeper understanding of "what is the principle of pyhton data mining self". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.