Recording the Way

About Python Class's '__Init__'

2017/12/28

Code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Vehicle(object):
"""Docstring"""
def __init__(self, color, doors, tires, vtype):
"""Constructor"""
self.color = color
self.doors = doors
self.tires = tires
self.vtype = vtype
def brake(self):
"""
Stop the car
"""
return "%s braking" % self.vtype
def drive(self):
"""
Drive the car
"""
return "I'm driving a %s %s!" % (self.color, self.vtype)
if __name__ == "__main__":
car = Vehicle("blue", 5 , 4, "car")
print(car.brake)
print(car.drive)

从中可以看出, init再次用于初始化class的参数用,或许这就是init最主要的用途,也就是对class参数的初始化。

CATALOG