- The overall introduction is here
- Introduction to Python
We end our journey in Function and step into Class in python now.
We can understand Class in the following 2 ways:
- A class is a bundle of data
- A class is a abstract class of data
1. What is a Class?
Supposed that we want to represent a Car and store some information of a car.
pythoncar_speed = 50 car_weight = 3 car_height = 2 def speed_up(): car_speed += 10Now we use 3 variables to store the basic information of a car.
What if we have 10 car, and each car has the same 3 variables assigned with different values?
pythoncar1_speed = 50 car1_weight = 3 car1_height = 2 def speed_up(): car1_speed += 10 car2_speed = 20 car2_weight = 2.5 car2_height = 2.1 def speed_up(): car2_speed += 15 car3_speed = 10 car3_weight = 1.2 car3_height = 1.6 def speed_up(): car3_speed += 20 ...Troublesome, right?
We now introduce the concept of Class--a bundle of data.
pythonclass Car: def __init__(self, speed, weight, height): self.speed = speed self.weight = weight self.height = height def speed_up(plus): self.speed += plus car1 = Car(20, 2, 1.5) car2 = Car(10, 2.5, 1.2) ...The grammar seems a little bit messy. Now I would like to explain the concept of it.
2. Concept of Class
A class contains the following several elements:
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
| class name | the name of a class, the naming rule is equal to common variable |
| class member variable | A class should contain some variables to charactorize it, and we call this kind of variables "attribute" |
| class member funciton | A class should contain some functions to implement some function, such as speed up a car. We call this kind of funcitons "method" |As we can see, a class is a template of data. For example, the class Car declare that a car should contains 3 variables (attributes ) and 1 function (methd).
Now with the class Car, we can generate some real cars in the code. We call this process "instantiation ". The meaning is clear: the class is set of regulation stipulating how data should be arranged in memory and the instance of the class is a real bundle of data.
pythoncar1 = Car(20, 2, 1.5) car2 = Car(10, 2.5, 1.2)Now the car1 and car2 are 2 references pointiing to 2 different instances of class Car.
3. Standard grammar of class
From discussion above, we know the concept of a class. Now let's see how to definie and use a class properly.
3.1 Definition of class
pythonclass Car: -------------------------> class name def __init__(self, speed): ----->special method self.speed = speed --------->create an attribute named speed def speed_up(): ---------->define a method self.speed += 10 ---------->change the attribute of the instance ======================================================================================= #the self keyword means the instance of Car car1 = Car(10) car2 = Car(20) car1 [speed self] | car1 car2 [speed self] | car2 !!All method in the class should contain the self as a parameter ===================================================================================== #the __init__(self, speed) is a special method #when an instance is created, the method will be called. car1 = Car(10) car2 = Car(20) car1 [speed self] | | 10 car1 car2 [speed self] | | 20 car23.2 Usage of class
We can use the class as a type of variable.
cppcar1 = Car(10)When the statement is implemented, the interpreter will allocate a section of memory to store a bundle of data, including the speed of the car1.
By the way
- self is a necessary parameter when we define a class method, yet, will not appear when the function is called. That is, we needn't pass any instantiations.
- We usually call the instantiation an Object.