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.

1. The attribute attribute is "information you know" about an object objec t (instance instance), and attributes are variables contained in the object/instance.

2. Method method is the action that can be done to the object/instance. method is a function contained in the object/instance.

3. If you grill a second kielbasa, you need to instantiate the second kielbasa first; one kielbasa cannot be grilled a second time.

4. A self pointer is a designator that points to an entity.

5. In Python and Java, self is also not a pointer anymore, it's an instance instance. But it's better understood as a pointer. A pointer and an instance are actually one thing, with the pointer being a better representation of what the pointing means. (@Chin Rowen points out that #5 is misunderstood)

5.Corrected to understand: Python treats all data, everything, as objects, and in Python there are no variables, only pointers (not in the sense that there are really no variables, but in the sense of understanding why the Python language is designed that way).

Assigning a variable is not defining a variable, it is defining a pointer, and to say that a variable is also a pointer variable. (Haven't read the Python Learning Manual yet, and fail to understand the design logic of the programming language at a deep level, so dig in first.

Slow Shift Zero-Based Python Learning Diary

If you're reading this, you can Leave a comment below.

Let's get together a Python learning supervision group

1、Photoshop release 191 can now key in one click
2、Fractions are not a panacea
3、Invited CrossStraits AI and Optoelectronics Industry Matchmaking Meeting for Talent and Projects will be held welcome to attend the meeting
4、JS Weekly 413 JavaScript Annual Trends 2018 The Ultimate Guide to npm Transcoding and Publishing ES2018 npm Modules with Babel7
5、A browser made for efficiency and simplicity get to know it Uninstallation is on me

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送