python 运算符重载

Python 运算符重载是指在类中重新定义运算符的行为,使其适用于自定义对象。Python 语言支持多种运算符重载,包括算术运算符、比较运算符、逻辑运算符等。通过运算符重载,可以使自定义对象具有与内置对象相同的行为,从而增强代码的可读性和可维护性。

_x000D_

算术运算符重载

_x000D_

Python 支持对加、减、乘、除等算术运算符进行重载,以适应自定义对象的运算需求。例如,可以定义一个名为 Vector 的类,表示二维向量,并对加、减、乘等运算符进行重载,实现向量的加减乘除等运算。

_x000D_

`python

_x000D_

class Vector:

_x000D_

def __init__(self, x, y):

_x000D_

self.x = x

_x000D_

self.y = y

_x000D_

def __add__(self, other):

_x000D_

return Vector(self.x + other.x, self.y + other.y)

_x000D_

def __sub__(self, other):

_x000D_

return Vector(self.x - other.x, self.y - other.y)

_x000D_

def __mul__(self, other):

_x000D_

return Vector(self.x * other, self.y * other)

_x000D_

def __truediv__(self, other):

_x000D_

return Vector(self.x / other, self.y / other)

_x000D_

v1 = Vector(1, 2)

_x000D_

v2 = Vector(3, 4)

_x000D_

print(v1 + v2) # 输出 Vector(4, 6)

_x000D_

print(v1 - v2) # 输出 Vector(-2, -2)

_x000D_

print(v1 * 2) # 输出 Vector(2, 4)

_x000D_

print(v1 / 2) # 输出 Vector(0.5, 1.0)

_x000D_ _x000D_

比较运算符重载

_x000D_

Python 支持对相等、不等、大于、小于、大于等于、小于等于等比较运算符进行重载。例如,可以定义一个名为 Complex 的类,表示复数,并对相等、不等等运算符进行重载,实现复数的相等比较。

_x000D_

`python

_x000D_

class Complex:

_x000D_

def __init__(self, real, imag):

_x000D_

self.real = real

_x000D_

self.imag = imag

_x000D_

def __eq__(self, other):

_x000D_

return self.real == other.real and self.imag == other.imag

_x000D_

def __ne__(self, other):

_x000D_

return not self.__eq__(other)

_x000D_

c1 = Complex(1, 2)

_x000D_

c2 = Complex(1, 2)

_x000D_

c3 = Complex(3, 4)

_x000D_

print(c1 == c2) # 输出 True

_x000D_

print(c1 != c3) # 输出 True

_x000D_ _x000D_

逻辑运算符重载

_x000D_

Python 支持对逻辑与、逻辑或、逻辑非等逻辑运算符进行重载。例如,可以定义一个名为 Box 的类,表示一个立方体,对逻辑非运算符进行重载,实现对立方体是否为空的判断。

_x000D_

`python

_x000D_

class Box:

_x000D_

def __init__(self, length, width, height):

_x000D_

self.length = length

_x000D_

self.width = width

_x000D_

self.height = height

_x000D_

def __bool__(self):

_x000D_

return self.length > 0 and self.width > 0 and self.height > 0

_x000D_

box1 = Box(0, 0, 0)

_x000D_

box2 = Box(1, 2, 3)

_x000D_

if box1:

_x000D_

print("Box1 is not empty")

_x000D_

else:

_x000D_

print("Box1 is empty")

_x000D_

if box2:

_x000D_

print("Box2 is not empty")

_x000D_

else:

_x000D_

print("Box2 is empty")

_x000D_ _x000D_

问答扩展

_x000D_

1. 什么是运算符重载?

_x000D_

运算符重载是指在类中重新定义运算符的行为,使其适用于自定义对象。Python 语言支持多种运算符重载,包括算术运算符、比较运算符、逻辑运算符等。

_x000D_

2. 运算符重载有哪些好处?

_x000D_

通过运算符重载,可以使自定义对象具有与内置对象相同的行为,从而增强代码的可读性和可维护性。例如,可以定义一个名为 Vector 的类,表示二维向量,并对加、减、乘等运算符进行重载,实现向量的加减乘除等运算。

_x000D_

3. 运算符重载的注意事项有哪些?

_x000D_

运算符重载应该遵循一些规则,以保证代码的正确性和可读性。应该避免过度使用运算符重载,以免造成代码混乱。应该遵循运算符的原有语义,避免对运算符的行为进行过度修改。应该遵循 Python 的惯例,例如使用 __add__ 表示加法运算符,使用 __eq__ 表示相等比较运算符等。

_x000D_

4. 运算符重载是否可以被继承?

_x000D_

运算符重载可以被继承,子类可以继承父类的运算符重载方法,并且可以对其进行修改或扩展。例如,可以定义一个名为 Point 的类,表示二维坐标点,继承 Vector 类,并对其进行修改,实现坐标点的加减乘除等运算。

_x000D_

`python

_x000D_

class Point(Vector):

_x000D_

def __init__(self, x, y):

_x000D_

super().__init__(x, y)

_x000D_

def __mul__(self, other):

_x000D_

return Point(self.x * other, self.y * other)

_x000D_

p1 = Point(1, 2)

_x000D_

p2 = Point(3, 4)

_x000D_

print(p1 + p2) # 输出 Vector(4, 6)

_x000D_

print(p1 - p2) # 输出 Vector(-2, -2)

_x000D_

print(p1 * 2) # 输出 Point(2, 4)

_x000D_

print(p1 / 2) # 输出 Point(0.5, 1.0)

_x000D_ _x000D_

5. 运算符重载是否可以被重载?

_x000D_

运算符重载可以被重载,即可以在已经重载的运算符方法中再次进行运算符重载。例如,可以在 Vector 类中对加法运算符进行二次重载,实现向量的叉乘运算。

_x000D_

`python

_x000D_

class Vector:

_x000D_

def __init__(self, x, y):

_x000D_

self.x = x

_x000D_

self.y = y

_x000D_

def __add__(self, other):

_x000D_

return Vector(self.x + other.x, self.y + other.y)

_x000D_

def __sub__(self, other):

_x000D_

return Vector(self.x - other.x, self.y - other.y)

_x000D_

def __mul__(self, other):

_x000D_

return Vector(self.x * other, self.y * other)

_x000D_

def __truediv__(self, other):

_x000D_

return Vector(self.x / other, self.y / other)

_x000D_

def __add__(self, other):

_x000D_

return self.x * other.y - self.y * other.x

_x000D_

v1 = Vector(1, 2)

_x000D_

v2 = Vector(3, 4)

_x000D_

print(v1 + v2) # 输出 -2

_x000D_ _x000D_
申请14天超长免费试听资格
获取500G教程资料
姓名
电话
课程
立即申请