class class, method, instance, pointer self
Slow and more zero-based python learning diary
Thanks to @yangYuan祐 How to understand classes and objects in Python?
Thanks to @WangdiZheng for patiently using the story of God's creation of man to explain how to create classes, create methods, and point to instances with the SELF pointer concept.
Suppose God wanted to create man. He built a human model which is also a model of a human being.
class human mod
One day, God wanted to produce humans from this mold, and then, He created Adam and Eve from this mold. Adam and Eve were examples of this mold, and Adam and Eve also belonged to the model created as the human mold. (Most books use the term "blueprint" as an analogy to "class")
Adam = human model; Eve = human model
One day, God wanted to do Adam and Eve's hair, so he had to use the do hair function. Luckily, God was resourceful enough to design the human mold with the do hair function set aside, creating two different methodsmethod long ago. SELF . Haircut = cut x cm; self . Dyeing your hair = dyeing it x color
Adam . Haircut (10cm cut)
Eve . Hair dye (dyed red)
However, when designing the human model, the model needs to indicate that the hair function is to do "one's own" hair, so how do we know that Adam cuts Adam's hair and Eve does Eve's hair? So inside this die, the function that does the hair (some method or several methodsMethod) is written as self . Shearhead = cut x cm; self . Dyeing your hair = dyeing it x color.
When Eve goes to call this method, inside the model, self becomes Eve, and Adam calls self to become Adam.
Each time the method is called it modifies the properties of the instance, for example the first call to Adam . Cut the hair (cut 10cm) and call Adam again for the second time. Cut the hair (cut 10cm), at which point Adam's hair is cut 20cm shorter. As for why the properties of the instance are modified every time the method is called? Why Python is designed this way is because it is the design idea of object-oriented languages, as is the case with almost all object-oriented languages, and also, modifying properties is embodied in instances.
(Personally, I find the story of God's creation more understandable than the "blueprint" explanation I've been reading before.)
Here's another example from The Programming Journey of Parents and Children that explains classes, methods, properties, and instances.