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.
相关推荐
京东云开发者13 分钟前
全球首个!京东全栈开源JoyAI-VL-Interaction,让大模型从“一问一答”走向“边看边说”
前端
京东云开发者14 分钟前
正式上线!京东云AI智能渗透测试服务
前端
AprChell18 分钟前
低代码设计器和低代码设计引擎架构综述
前端·vue.js·低代码
Hilaku23 分钟前
Node.js 还能再战十年?给你一个不换引擎的理由
前端·javascript·程序员
用户2986985301427 分钟前
Word 文档字符级格式化:Java 实现方案详解
java·后端
颜进强32 分钟前
AI性能参数-截断、延迟与流式输出
前端·后端·ai编程
spmcor39 分钟前
React 架构师之路:Next.js 全栈革命(第八篇)
前端·react.js
英勇无比的消炎药39 分钟前
TinyRobot 源码深度分析:OpenTiny 的 AI 对话组件库
前端·vue.js·github
假如让我当三天老蒯40 分钟前
React基础、进阶(学习用)
前端·react.js·面试
曲幽41 分钟前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict