当前位置:首页>python>第09篇|Python 面向对象编程:用前端组件思维来理解 Class

第09篇|Python 面向对象编程:用前端组件思维来理解 Class

  • 2026-09-08 15:41:30
第09篇|Python 面向对象编程:用前端组件思维来理解 Class

这篇文章用"类 = 组件模板,实例 = 组件实例"的前端思维,带你彻底理解 Python OOP——类定义、构造函数、三种方法、继承、魔术方法,每个概念都有 JS 对照。

前言

进入第二阶段了!如果说基础篇是打地基,进阶篇就是开始建楼。

面向对象编程(OOP)是 Python 里绕不过去的一关。好消息是:你早就用过 OOP 了。

React 的 class 组件、JS 的 class 语法、new Date()、new Map()——这些都是面向对象编程。Python 的 class 和 JS 的 class 在概念上几乎一致,语法细节稍有不同。

最有用的类比:类 = 组件模板,实例 = 挂载的组件。你写一个 <Button> 组件(类),页面上可以渲染出任意多个按钮(实例),每个按钮有自己的 props(属性)和 state(状态)。

这篇文章覆盖:

  • 类的定义与实例化(对比 JS class)
  • 构造函数 __init__(对比 constructor)
  • 实例方法 / 类方法 / 静态方法
  • 继承与 super()
  • 常用魔术方法(__str__、__len__、__eq__)

一、定义类与创建实例

1.1 基本语法

# Python 类定义class Button:    def __init__(self, label, color="blue"):        self.label = label    # 实例属性        self.color = color    def click(self):        print(f"点击了 [{self.label}] 按钮")# 创建实例(不需要 new 关键字!)btn1 = Button("确认")btn2 = Button("取消", color="red")btn1.click()   # 点击了 [确认] 按钮btn2.click()   # 点击了 [取消] 按钮print(btn1.label)   # 确认print(btn2.color)   # red

对比 JS:

class Button {  constructor(label, color = "blue") {    this.label = label    this.color = color  }  click() {    console.log(`点击了 [${this.label}] 按钮`)  }}const btn1 = new Button("确认")   // JS 需要 newconst btn2 = new Button("取消", "red")

核心差异对照:

概念
Python
JavaScript
类定义
class MyClass:class MyClass {}
构造函数
def __init__(self):constructor() {}
实例引用
self
(必须显式传入)
this
(隐式)
创建实例
obj = MyClass()const obj = new MyClass()

最重要的区别:Python 的 self

Python 所有实例方法的第一个参数必须是 self,代表当前实例(等同于 JS 的 this)。self 是约定俗成的名字,理论上可以叫别的,但强烈不建议。

class Dog:    def __init__(self, name):        self.name = name   # self.name 是实例属性    def bark(self):        print(f"{self.name} 说:汪汪!")   # 通过 self 访问实例属性dog = Dog("旺财")dog.bark()   # 旺财 说:汪汪!

二、三种方法:实例方法 / 类方法 / 静态方法

Python 类中有三种不同类型的方法,这是 JS class 里没有直接对应的概念:

class Circle:    pi = 3.14159   # 类属性(所有实例共享)    def __init__(self, radius):        self.radius = radius   # 实例属性(每个实例独有)    # 1. 实例方法(最常用):操作实例数据,第一个参数是 self    def area(self):        return self.pi * self.radius ** 2    # 2. 类方法:操作类本身,第一个参数是 cls(代表类)    @classmethod    def from_diameter(cls, diameter):        """通过直径创建圆(替代构造函数)"""        return cls(diameter / 2)    # 3. 静态方法:和类相关但不需要访问类或实例,没有特殊第一参数    @staticmethod    def is_valid_radius(radius):        """验证半径是否合法"""        return radius > 0
# 使用示例c1 = Circle(5)print(c1.area())           # 78.53975(实例方法)c2 = Circle.from_diameter(10)  # 类方法(工厂方法模式)print(c2.radius)           # 5.0print(Circle.is_valid_radius(-1))  # False(静态方法)

什么时候用哪种:

  • 实例方法:大多数情况,需要读写 self 的属性
  • 类方法:工厂方法(提供多种创建实例的方式)、操作类属性
  • 静态方法:工具函数,逻辑上属于这个类,但不依赖实例或类的数据

三、继承与 super()

class Animal:    def __init__(self, name, age):        self.name = name        self.age = age    def speak(self):        print(f"{self.name} 发出了声音")    def info(self):        print(f"姓名:{self.name},年龄:{self.age}")class Dog(Animal):   # 括号里写父类名    def __init__(self, name, age, breed):        super().__init__(name, age)   # 调用父类构造函数        self.breed = breed            # 子类新增属性    def speak(self):   # 覆盖父类方法        print(f"{self.name} 说:汪汪!")    def fetch(self):   # 子类新增方法        print(f"{self.name} 去捡球了!")class Cat(Animal):    def speak(self):        print(f"{self.name} 说:喵喵!")
dog = Dog("旺财", 3, "金毛")dog.speak()    # 旺财 说:汪汪!(覆盖了父类方法)dog.info()     # 姓名:旺财,年龄:3(继承自父类)dog.fetch()    # 旺财 去捡球了!cat = Cat("咪咪", 2)cat.speak()    # 咪咪 说:喵喵!# 检查继承关系print(isinstance(dog, Dog))     # Trueprint(isinstance(dog, Animal))  # True(也是 Animal 的实例)print(issubclass(Dog, Animal))  # True

对比 JS 继承:

class Animal {  constructor(name, age) {    this.name = name    this.age = age  }  speak() { console.log(`${this.name} 发出了声音`) }}class Dog extends Animal {  constructor(name, age, breed) {    super(name, age)   // 和 Python 完全一致    this.breed = breed  }  speak() { console.log(`${this.name} 说:汪汪!`) }}

继承语法对比:Python 用 class Dog(Animal),JS 用 class Dog extends Animal,其余几乎相同。


四、魔术方法(Dunder Methods)

魔术方法是 Python OOP 最有特色的部分,也是 JS 里没有直接对应物的特性。

魔术方法名称前后各有两个下划线(__method__),Python 在特定情况下会自动调用它们。

4.1 __str__:对象的字符串表示

class User:    def __init__(self, name, email):        self.name = name        self.email = email    def __str__(self):        """print() 时调用,面向用户的友好描述"""        return f"用户:{self.name} ({self.email})"    def __repr__(self):        """调试时显示,面向开发者的精确描述"""        return f"User(name='{self.name}', email='{self.email}')"user = User("Alice", "alice@example.com")print(user)         # 用户:Alice (alice@example.com)  ← __str__print(repr(user))   # User(name='Alice', email='alice@example.com')  ← __repr__

没有 __str__ 时,print(user) 输出的是 <__main__.User object at 0x10f3a2d90> 这样的内存地址,完全不友好。

4.2 __len__:支持 len() 函数

class Playlist:    def __init__(self, name):        self.name = name        self.songs = []    def add(self, song):        self.songs.append(song)    def __len__(self):        return len(self.songs)playlist = Playlist("我的歌单")playlist.add("稻香")playlist.add("晴天")playlist.add("七里香")print(len(playlist))   # 3

4.3 __eq__:自定义相等比较

class Point:    def __init__(self, x, y):        self.x = x        self.y = y    def __eq__(self, other):        return self.x == other.x and self.y == other.y    def __str__(self):        return f"Point({self.x}, {self.y})"p1 = Point(1, 2)p2 = Point(1, 2)p3 = Point(3, 4)print(p1 == p2)   # True(使用了 __eq__)print(p1 == p3)   # False

没有 __eq__ 时,p1 == p2 比较的是内存地址,两个内容相同的对象也会返回 False。

4.4 常用魔术方法速查

魔术方法
触发时机
类比
__init__
创建实例时
constructor
__str__print()
 或 str()
toString()
__repr__
调试时的对象描述
无直接对应
__len__len()
 函数
无直接对应
__eq__==
 比较
无直接对应(JS 用 valueOf)
__lt__<
 比较(支持排序)
无直接对应
__add__+
 运算符
无直接对应
__getitem__obj[key]
 下标访问
无直接对应
__iter__for ... in
 迭代
[Symbol.iterator]
__enter__
 / __exit__
with
 语句
无直接对应

五、综合实战:购物车类

把本篇所有知识点融合到一个真实场景里:

"""购物车系统示例---本模块实现了一个简易的电商购物车功能,包含:  - Product 类:商品模型,表示单个商品的信息(名称、价格、库存)  - Cart 类:购物车模型,支持添加、移除商品,并计算总价"""class Product:    """    商品类(Product)    用于表示商城中的单个商品,包含商品名称、价格和库存数量。    """    def __init__(self, name: str, price: float, stock: int = 0):        """        初始化商品实例。        :param name: 商品名称        :param price: 商品单价(元)        :param stock: 库存数量,默认为 0        """        self.name = name      # 商品名称        self.price = price    # 商品单价        self.stock = stock    # 当前库存数量    def __str__(self):        """        魔术方法:定义对象的字符串表示形式。        当使用 print() 或 str() 时调用,返回 "商品名 ¥价格" 格式。        """        return f"{self.name} ¥{self.price:.2f}"    def __eq__(self, other):        """        魔术方法:定义相等比较(==)的行为。        两个商品若名称相同则视为同一商品(用于购物车中判断是否重复添加)。        """        return self.name == other.nameclass Cart:    """    购物车类(Cart)    代表某个用户的购物车,支持添加商品、移除商品、查看总价等操作。    """    def __init__(self, user: str):        """        初始化购物车。        :param user: 购物车所属用户的名称        """        self.user = user        # 购物车商品列表,每项为字典:{"product": 商品对象, "qty": 数量}        self.items: list[dict] = []    def add(self, product: Product, qty: int = 1) -> None:        """        将商品加入购物车。        :param product: 要添加的商品对象        :param qty: 添加数量,默认为 1        :raises ValueError: 当请求数量超过库存时抛出        """        # 库存校验:请求数量不能超过当前库存        if product.stock < qty:            raise ValueError(f"库存不足,{product.name} 仅剩 {product.stock} 件")        # 若该商品已在购物车中,则增加数量而非新增一项        for item in self.items:            if item["product"] == product:                item["qty"] += qty                return        # 新商品则追加到列表末尾        self.items.append({"product": product, "qty": qty})    def remove(self, product: Product) -> None:        """        从购物车中移除指定商品(不论数量多少,全部移除)。        :param product: 要移除的商品对象        """        # 使用列表推导式过滤掉与 product 相同的商品项        self.items = [i for i in self.items if i["product"] != product]    @property    def total(self) -> float:        """        计算购物车中所有商品的总价(只读属性)。        @property 装饰器使方法可以像属性一样访问,如 cart.total 而非 cart.total()        :return: 总价(元)        """        return sum(i["product"].price * i["qty"] for i in self.items)    def __len__(self):        """        魔术方法:定义 len() 的返回值。        返回购物车中商品种类数(不同商品的个数)。        """        return len(self.items)    def __str__(self):        """        魔术方法:定义购物车的字符串表示。        返回格式化的购物车清单及合计金额。        """        if not self.items:            return f"{self.user} 的购物车(空)"        lines = [f"{self.user} 的购物车:"]        for item in self.items:            lines.append(f"  · {item['product']} × {item['qty']}")        lines.append(f"  合计:¥{self.total:.2f}")        return "\n".join(lines)# ==================== 示例使用 ====================# 创建两个商品实例:Python编程书(89.9元,库存10)和机械键盘(299元,库存3)p1 = Product("Python编程书", 89.9, stock=10)p2 = Product("机械键盘", 299.0, stock=3)# 为用户 Alice 创建购物车,并添加商品cart = Cart("Alice")cart.add(p1, 2)   # 添加 2 本 Python 编程书cart.add(p2, 1)   # 添加 1 个机械键盘# 打印购物车内容(会调用 Cart 的 __str__ 方法)print(cart)# 预期输出:# Alice 的购物车:#   · Python编程书 ¥89.90 × 2#   · 机械键盘 ¥299.00 × 1#   合计:¥478.80# 使用 __len__ 获取商品种类数;使用 total 属性获取总价print(f"共 {len(cart)} 种商品")   # 输出:共 2 种商品print(f"总价:¥{cart.total:.2f}")  # 输出:总价:¥478.80

小结

概念
Python
JavaScript
类定义
class Foo:class Foo {}
构造函数
def __init__(self):constructor() {}
实例引用
self
(显式参数)
this
(隐式)
继承
class Dog(Animal):class Dog extends Animal
调用父类
super().__init__()super()
字符串表示
__str__toString()
运算符重载
__add__
、__eq__ 等
基本不支持

3 个容易踩的坑:

  1. 实例方法忘写 self 参数——Python 不会自动补,会报 TypeError
  2. 子类 __init__ 里忘记调用 super().__init__()——父类属性不会被初始化
  3. 修改类属性时直接 self.class_attr = x 实际上是创建了实例属性,没有修改类属性

下篇预告

第 10 篇:Python 装饰器:前端最陌生但最值得学的语法糖

你刚刚见到了 @classmethod、@staticmethod、@property ——这些都是装饰器。下一篇我们深入讲装饰器的原理和自定义写法,理解了它,FastAPI 的路由代码就再也不神秘了。


最新文章

随机文章