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.
相关推荐
小叶肥辉几秒前
LangChain链和LangGraph图的学习笔记【四】——(1)调用方法:invoke(2)提示语模板:PromptTemplate
python·langchain·prompt
lv__pf1 分钟前
09-深入理解AQS之独占锁ReentrantLock源码分析【TL 并发 09】
java·开发语言
letisgo52 分钟前
JAVA 高级进阶18篇《生产级RAG:切分、检索、评估与Graph RAG全链路实战》
java·面试·检索增强·rag·graph rag
福兮说4 分钟前
纯前端把图片压缩到指定体积:canvas.toBlob 配合二分查找
前端·javascript·canvas·图片处理
随遇而安zx5 分钟前
【并发】---Disruptor 原理、源码与实战 深度解析
java·并发·disruptor
Lyyaoo.9 分钟前
【二分查找】【困难】寻找两个有序数组的中位数
java·数据结构·算法
小兔崽子去哪了10 分钟前
深度学习 2 / CNN 神经网络
后端·python
是立不是利10 分钟前
CSS 入门与进阶:从选择器到响应式布局的完整指南
前端·css
镭立智能制造16 分钟前
电线电缆换线频繁?MES系统如何缩短换线时间40%
前端·制造
weixin_4407305019 分钟前
队列QUEUE介绍+类型示例(先进先出、后进先出、优先级高先出、双端进入、生产者+消费者线程)
python·线程·queue