Python hầu như implement mọi code dùng class. Programmer dùng class để gom code liên quan. Dùng keyword class - collection của object-oriented constructs.
Class là template tạo object. Object có member variable và behavior. Trong Python, class tạo bằng keyword class.
Ví dụ: Class rỗng đơn giản
class Cars:
pass
Keyword class theo sau là tên class (chữ cái đầu viết hoa), rồi dấu hai chấm : kết thúc khai báo.
Class có method và attribute
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def start(self):
print(f"{self.brand} {self.model} started")
my_car = Car("Toyota", "Camry")
my_car.start() # Toyota Camry started