简介
Python 运算符重载为开发者提供了一种强大的机制,用于在他们的类中定义标准运算符的自定义行为。本教程探讨了实现运算符重载的基本技术和实用模式,使程序员能够通过重新定义对象与标准 Python 运算符的交互方式,创建更具表现力和直观性的代码。
Python 运算符重载为开发者提供了一种强大的机制,用于在他们的类中定义标准运算符的自定义行为。本教程探讨了实现运算符重载的基本技术和实用模式,使程序员能够通过重新定义对象与标准 Python 运算符的交互方式,创建更具表现力和直观性的代码。
运算符重载是 Python 中的一项强大功能,它允许开发者在将内置运算符用于用户定义的类时定义自定义行为。这种机制使对象能够以直观且有意义的方式与标准运算符进行交互。
运算符重载让你能够重新定义运算符对自定义类的工作方式,使你的代码更具可读性和表现力。通过实现特殊方法,你可以控制像 +、-、*、== 等运算符在与你的对象交互时的行为。
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
## 使用示例
v1 = Vector(2, 3)
v2 = Vector(4, 5)
result = v1 + v2
print(result) ## 输出:Vector(6, 8)
| 运算符 | 魔法方法 | 描述 |
|---|---|---|
+ |
__add__() |
加法 |
- |
__sub__() |
减法 |
* |
__mul__() |
乘法 |
== |
__eq__() |
相等性比较 |
< |
__lt__() |
小于比较 |
运算符重载有以下几个好处:
通过掌握运算符重载,开发者可以在 Python 中创建更具表现力和直观性的类。LabEx 建议练习这些技术以提升你的编程技能。
魔法方法,也称为双下划线方法(dunder methods),是 Python 中的特殊方法,用于定义对象在与内置操作和语法交互时的行为方式。
__init__() 方法class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag
__new__() 方法class Singleton:
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
| 方法 | 用途 | 示例 |
|---|---|---|
__str__() |
人类可读的字符串表示 | print(object) |
__repr__() |
详细的字符串表示 | repr(object) |
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}, {self.age} 岁"
def __repr__(self):
return f"Person('{self.name}', {self.age})"
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __eq__(self, other):
return self.width * self.height == other.width * other.height
def __lt__(self, other):
return self.width * self.height < other.width * other.height
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
class CustomList:
def __init__(self, items):
self.items = items
def __len__(self):
return len(self.items)
def __getitem__(self, index):
return self.items[index]
def __setitem__(self, index, value):
self.items[index] = value
class FileManager:
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.file = open(self.filename, 'r')
return self.file
def __exit__(self, exc_type, exc_value, traceback):
self.file.close()
LabEx 建议掌握魔法方法,以创建更强大、更直观的 Python 类。
class Money:
def __init__(self, amount, currency='USD'):
self.amount = amount
self.currency = currency
def __add__(self, other):
if self.currency!= other.currency:
raise ValueError("不同货币不能相加")
return Money(self.amount + other.amount, self.currency)
def __mul__(self, factor):
return Money(self.amount * factor, self.currency)
def __str__(self):
return f"{self.currency} {self.amount:.2f}"
class CustomDict:
def __init__(self):
self._data = {}
def __getitem__(self, key):
return self._data.get(key, '未找到')
def __setitem__(self, key, value):
self._data[key] = value
def __len__(self):
return len(self._data)
def __iter__(self):
return iter(self._data)
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def __eq__(self, other):
return self.grade == other.grade
def __lt__(self, other):
return self.grade < other.grade
def __gt__(self, other):
return self.grade > other.grade
class Temperature:
def __init__(self, celsius):
self.celsius = celsius
def __float__(self):
return self.celsius
def __int__(self):
return int(self.celsius)
def __str__(self):
return f"{self.celsius}°C"
class ValidatedAttribute:
def __init__(self, validator):
self.validator = validator
def __set_name__(self, owner, name):
self.name = name
def __get__(self, instance, owner):
return instance.__dict__.get(self.name)
def __set__(self, instance, value):
if not self.validator(value):
raise ValueError(f"{self.name} 的值无效")
instance.__dict__[self.name] = value
| 模式 | 魔法方法 | 使用场景 |
|---|---|---|
| 数值模拟 | __add__, __mul__ |
自定义数值类型 |
| 比较 | __eq__, __lt__, __gt__ |
对对象进行排序 |
| 容器 | __getitem__, __len__ |
自定义集合类型 |
| 转换 | __float__, __int__ |
类型转换 |
class SafeDivision:
def __init__(self, value):
self.value = value
def __truediv__(self, other):
try:
return SafeDivision(self.value / other.value)
except ZeroDivisionError:
return SafeDivision(0)
LabEx 建议掌握这些模式,通过智能的运算符重载创建更健壮、更灵活的 Python 类。
通过掌握 Python 运算符重载,开发者能够创建出更复杂、更灵活的类,使其与 Python 内置运算符无缝集成。理解魔法方法并实现自定义运算符行为,能够编写出更优雅、更具可读性的代码,最终提升 Python 中面向对象编程的整体设计和功能。