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.
相关推荐
whale fall2 小时前
celery -A tool.src.main worker --loglevel=info --queues=worker1_queue & 什么意思
python·学习·apache
naruto_lnq2 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
喵手2 小时前
Python爬虫实战:采集博客园 Cnblogs文章标题、发布日期、标签以及HTML正文等(附 Markdown 文档格式预览)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·博客园文章采集·博客园文章采集转md格式
小邓吖2 小时前
自己做了一个工具网站
前端·分布式·后端·中间件·架构·golang
OLOLOadsd1232 小时前
柑橘类水果病害识别与分级_faster-rcnn_hrnetv2p-w32-1x_coco实现
python
南风知我意9572 小时前
【前端面试2】基础面试(杂项)
前端·面试·职场和发展
qq_12498707532 小时前
基于Srpingboot心晴疗愈社平台的设计与实现(源码+论文+部署+安装)
java·数据库·spring boot·spring·microsoft·毕业设计·计算机毕业设计
大爱编程♡3 小时前
SpringBoot统一功能处理
java·spring boot·后端