Preface : I am a python beginner . When learning to Python classes and objects, the concept of ' advanced ' was abused. Ask questions on the Internet, get the reply is almost ' big God ' hid with the necessity to squeeze out the words, the Internet search articles, long length, those characters are recognized, but just do not know what meaning, and are repeatedly copied and pasted resulting in the article typesetting extremely spicy eyes.
A gloomy winter, I found a video author John Philip Jones on YouTube and discovered a tutorial on Python classes and objects . This is the best way I've ever seen to learn python in small white . Because in the domestic YouTube inconvenient, intends to write an article to carry over the content, while adding some of their own learning experience, hope to be like me small white in the small white to provide this information.
Copyright author, repeat three times: John Philip Jones, John Philip Jones,john Philip Jones
First of all, I've already assumed that you've looked at countless spam articles and still don't understand classes and objects, but at least you know that there are two of things like classes and objects.
Since you have no programming experience, you cannot understand Python's classes and objects by analogy in a learned programming language. Let's use the example of life to say, build a house.
Class: Equivalent to construction drawings (blueprint)
Objects (object): House (already built)
Suppose you have a construction drawing (Blueprint) with all the information about the house (cover a few floors, where the kitchen is, where the bedroom is, how to build it).
You have been looking forward to your life in this new home, buy expensive furniture, in the kitchen to eat the DA ... However, these are your yy, you have to do these things must wait until the house is really built later. If the house in the middle of rotten tail, you can not do anything, dreams are nowhere to be placed.
So it's important to say, ' A built house '. In Python, ' Everything is Object ', which is what we call object oriented. with the object, we can do things!!!
Then come:
For the building of the house may be slightly different, everyone has their own preferences, as seen in the picture, may be the house of the same type, but the color is different. emphasis: They are all made according to the ' construction drawings '.
----------------------------------------------------------
The example ends with the ' spirit ' that I want to express in the example, and we go into Python to see ' Classes and Objects ':
To assign a value to a variable: a = 2
It is said that according to our general understanding, create a variable named a, and then assign a value of 2. The equivalent of putting 2 into a basket called a, blablablabla ...
Roll it! Let's consider the question of a = 2 from another angle this time, and start with:
When you hit a = 2 in Python, then enter. Python has an ' integer class' thing called ' wake '.
The integer class will then ' Create ' an object in the execution area, which is created from the construction drawings of the integer class. So, the object (the round thing in the picture) has an integer class feature or attribute. (The process is that you see the house is almost ready to build)
Do you remember the A in the A = 2? Where is it going?
Because the statement, a is to be equal to (=) 2, because you pressed the carriage return, haha
So...........
A will be used as a label to hook up with this object (round things). (The room has been built, you can take the key to live in it)
------------------------------------------------
The old Wang in the next room also bought a house!?!? At this point, another object (round things) is created.
b = 3
Lao Wang took the key to live in , b as the label is tied to the new object.
Finally, say a few more points:
You can see that each time you create objects (round things), their IDs are different.
Then goose ..... If we change B = 3 to b =2, eh? Don't we already have a = 2? What about the snow in front of the sweeper?
This means that the old king of the next door will reach out ...
When you change to B = 2, the integer class does not recreate an object. Instead, the B (the old king next door) is tagged and tied to the object (A = 2) that was previously created. (Please see)
Finally, two sentences: In Python, for a = 2, do not think of the value of 2 into a basket!!!
should be a as a label, hanging on the object (value 2)!!! object is very important!!! That's the round thing in the picture!!!!!! object is very important!!!
So far, it's just a head start. It also involves a lot of operations on classes and objects, and the ID in each object is also important (referring to the method reference, and the self problem)
But once you have this idea, it's cool to write object-oriented programs, such as:
Python classes and objects entry-level tutorials (simple rough)