Yes, you read that right, it's the soulful question I asked when I was first learning python.
We'll always see self in class, but it doesn't feel like he's doing much, just sitting there taking up space.
If you have the same question, then congratulations, you didn't learn your CLASS.
So, before we explain who SELF is, let's clarify a few things:
- What is class and what is instance?
- What is an object, what is a method, what is a function?
On a side note, I am personally opposed to Chinese translations of proper nouns that are originally in English in programming. As the saying goes, language shapes thinking, so once some proper nouns are translated, no matter how well you translate them, there will always be a certain degree of semantic ambiguity. For example, "class", when we see this word, we will instantly associate it with "form of divination", but the translation is "resemble", so we will unconsciously follow the word "form of divination" or "form of divination", or the word "class" in the Chinese translation, or the word "class" in the Chinese translation. So we will unconsciously try to understand this proper noun as "class" or "a large class". This is a very serious and potentially misleading situation, because the noun class has very little to do with "class" or "a broad category". Therefore, it is better not to translate it, just pretend that you don't know what it means, and then understand what it stands for in the course of your study.
In fact, because of the limitations of the English language, a lot of programming languages have terms that are exclusive to them that the bigwigs just slap on the head and make up. For example, axes in matplotlib, which I've spit out countless times.
Too much talk again. Back on topic.
1. What is a class, what is an instance, and what is an object?
Class: can be thought of as an assembly factory. If we want to produce a robot, let's build a factory first. Let's make sure: we're going to install the arms first, then the head, and we've got an assembly line for our little broken robot. This factory is smarter, the number of arms and heads can be adjusted.
class BuildRobot(): def __init__(self,armcount,headcount): = armcount = headcount
So here's the class that's building a factory called BuildRobot.'___init__' is telling this pipeline that first you need how many arms ('armcount') and how many heads ('headcount') this robot has. Ignore the self here for now, we'll get to that later.
At this point you can run it so that your class is built. But at this point the factory, because you haven't started production, is not producing anything. Here is the instance
INSTANCE: can be understood as starting a factory once to produce a robot. Now we use the factory we built earlier to produce a normal robot with two arms and a head:
normal_robot = BuildRobot(2,1)
Check to see if the arm count is correct.
Let's have another not-so-normal robot:
weird_robot = BuildRobot(4,1)
normal_robot and weird_robot are both instances. they don't have the same number of arms, but they're both essentially robots built from this class, consisting of arms and a head.
object: this is a bit tricky, in most cases, object and instance mean the same thing, both refer to the robot that was built. 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
These two statements above are equivalent.
2. what is method and what is function?
Both are defined by def. A slightly rougher way to understand this is that a function inside a class is called a method. so a method is a kind of function related to a class, instance.
Take a chestnut:
Still the factory above, we are now adding a shop to take care of coloring the arms:
class BuildRobot(): def __init__(self,armcount,headcount): = armcount = headcount def paintarm(self,color): print("paint arm:",color)
The paintarm, which is a method, is still the same, and now the class is not being produced, so there is no actual product coming out of the method. We can only produce an instance first:
colorful_robot = BuildRobot(2,1)
Okay we now have a robot with two arms and a head. At this point, our robot is still not colored because we didn't get this instance into the workshop where it was colored. Now let's get this robot into the workshop and color it red.
colorful_robot.paintarm('red')
paint arm: red
The process above is call this paintarm method. a few points:
There is no way for this workshop to color the arms without first building a robot, so in order to color them, you have to build a robot out of it first. So, METHOD is dependent on INSTANCE.
This workshop can only color the arms of robots produced by this factory; he won't do it if you take a car from another factory and ask him to color it. Therefore, method is dependent on class. Only the instance created by the class can call the method.
Suppose I take the job of coloring and outsource it. I'd just build another factory out there that specializes in coloring, and that's 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, it doesn't matter what factory you got this thing from, whether you're a robot or a robot dog, I'm just going to bring it in and color the arm anyway.
When you see this, you should understand the difference between function and method.
Note that there are actually two kinds of method, an instance method and a class method.
- The instance method is the workshop equivalent of making all sorts of modifications to a robot as a product. If I color the robot, it doesn't affect the appearance of my factory, right?
- The class method, is a workshop that modifies the attributes 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 color attribute of the robot I build.
For this discussion, let's limit ourselves to the instance method.
3. Focused SELF analysis
Once you've understood class and method, you can finally talk about self. With the above example, we notice that the
outsourcing_paint(colorful_robot,'red')
- Inside the FUNCTION, there is no SELF. Because we tell the outsourcing 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 using method, we only told the workshop that I want red color. How does the workshop know which robot to color? Is it a normal robot or is it a colorful robot? Because when we call the method, we use the colorful_robot.paintarm() format, so the paintarm method knows, oh, I want to color this colorful_robot.
- In python, for the () format to work, when writing a method inside a class, you have to leave the first bit of the variable open to refer to the instance that will call the method in the future. it's the same as if we were building a workshop to color our arms, we had to reserve an entrance to put in the robot that has already been produced. robots that have already been produced.
- The bit that is left out can be called anything. It's just that for the sake of code elegance, most people choose to use self to refer to the instance using the method itself.
summarize
- self is a placeholder that is placed in the first position of the variable name when writing the instance method for the class.
- You can write the instance method without using the self variable.
- If you want to change an instance's attribute inside a method, you can use it to refer to that attribute for modification.
So self is the instance created by the class.
This is the detailed content of pyhton learning and data mining self principle and application analysis, more information about pyhton data mining self analysis please pay attention to my other related articles!