Class in 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.

python 复制代码
car_speed = 50
car_weight = 3
car_height = 2
def speed_up():    
    car_speed += 10

Now 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?

python 复制代码
car1_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.

python 复制代码
class 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.

python 复制代码
car1 = 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

python 复制代码
class 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   car2

3.2 Usage of class

We can use the class as a type of variable.

cpp 复制代码
car1 = 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.
相关推荐
恋猫de小郭15 小时前
iOS + AI ,国外一个叫 Rork Max 的项目打算替换掉 Xcode
android·前端·flutter
databook15 小时前
🚀 Manim CE v0.20.0 发布:动画构建更丝滑,随机性终于“可控”了!
python·动效
何中应15 小时前
使用Python统计小说语言描写的字数
后端·python
姜源Jerry15 小时前
【Trae】Trae IDE&SOLO浅尝
java·ide·ai
宇木灵16 小时前
C语言基础-三、流程控制语句
java·c语言·前端
喵手16 小时前
Python爬虫实战:网抑云音乐热门歌单爬虫实战 - 从入门到数据分析的完整指南!
爬虫·python·爬虫实战·网易云·零基础python爬虫教学·音乐热门采集·热门歌单采集
qq84061223316 小时前
Nodejs+vue基于elasticsearch的高校科研期刊信息管理系统_mb8od
前端·vue.js·elasticsearch
skywalk816316 小时前
LTX-2 是一个基于 Transformer 的视频生成模型,能够根据文本描述生成高质量视频
python·深度学习·transformer
不懒不懒17 小时前
【Python办公自动化进阶指南:系统交互与网页操作实战】
开发语言·python·交互