Python运算符重载
欢迎使用Python操作符重载教程。
正如我们已经了解Python类一样,今天我们将学习面向对象python的另一个有趣的功能。
Python运算符重载
Python运算符重载使我们能够像对待任何原始数据类型一样,在python对象上使用数学,逻辑和按位运算符。
例如,您可以使用+运算符轻松地将两个数字3和5相加,即3 +5。
结果为8。
但是,如果您想添加两个圆(Circle类的对象)并制作一个半径为两倍的圆,该怎么办?或者,如果您想添加两个笛卡尔网格点以使用相同的" +"运算符生成另一个点,该怎么办? Python运算符重载使您可以像执行这些操作一样执行操作。
Python运算符重载示例
现在,让我们看一个重载数学运算符的例子。
class GridPoint: #Line: 1, Declaring a class def __init__(self, x, y): self.x = x self.y = y #Line: 4 def __add__(self, other): # Equivalent of + operator return GridPoint(self.x + other.x, self.y + other.y) def __str__(self): #Line: 12, returns the attributes when the object is printed string = str(self.x) string = string + ", " + str(self.y) return string #Line: 12 point1 = GridPoint(3, 5) #Line: 14 Creating a grid point point2 = GridPoint(-1, 4) #Line: 15, Creating another point point3 = point1 + point2 #Line: 16, Add two points using __add__() method print(point3) #Line: 17, Print the attributes using __str__() method
第1至4行指示类GridPoint的声明和构造方法的定义。
让我们看一下第6行和第7行。
def __add__(self, other): # Equivalent of + operator return GridPoint(self.x + other.x, self.y + other.y)
当我们将" +"运算符用作数学加法运算时,会隐式调用__add __()方法。
因此,如果要添加GridPoint类的两个对象,则必须重新定义此方法。
因此,在这里我们需要创建另一个GridPoint类的实例,其x的值是在'+'运算符周围的两个GridPoint实例中x的总和,而y的值也是在''周围的两个GridPoint实例中y的总和。
+'运算符。
第9至12行定义了__str __()方法,当我们尝试打印对象时会调用该方法。
这也是一种内置方法。
但是我们将重载该方法,以便它以我们指定的格式打印。
第14和15行,我们创建了GridPoint的两个对象,即point1和point2。
现在,看第16行。
使用" +"运算符添加GridPoint类的两个实例,并将它们分配为GridPoint的另一个实例。
Python运算符重载可以帮助您做到这一点。
因此,当第17行显示如下图所示的输出时,请不要感到惊讶。
数学运算符列表
这是可以重载的运算符列表,并且可以类似的方式与python运算符重载一起使用。
Operator | Description | Method |
+ | Addition | add(self, other) |
- | Subtraction | sub(self, other) |
* | Multiplication | mul(self, other) |
/ | True Division | truediv(self, other) |
// | Floor Division | floordiv(self, other) |
% | Remainder | mod(self, other) |
** | Power | pow(self, other) |
& | Bitwise AND | and(self, other) |
| | Bitwise OR | or(self, other) |
^ | Bitwise XOR | xor(self, other) |
在Python中重载关系运算符
关系运算符在python中以非常相似的方式重载。
但是区别是,这些运算符通常返回true/false而不是对象的另一个实例。
让我们来看一个例子。
class GridPoint: def __init__(self, x, y): self.x = x self.y = y def __gt__(self, other): # Overloading the greater than operator return self.x > other.x # Returns true if value of x in the left operand is greater than that in the right one. Returns false otherwise def __str__(self): string = str(self.x) string = string + ", " + str(self.y) return string point1 = GridPoint(3, 5) point2 = GridPoint(-1, 4) if point1 > point2: # Compares with the overloaded __gt__() method print('point1 is greater than point2') else: print('point1 is not greater than point2')
查看第6行,已加载"大于"运算符。
如果左侧的操作数大于右侧的操作数,则传统的">"运算符将返回true。
我们将使用此属性来比较GridPoint类的两个实例。
然后,在第17行中,我们比较GridPoint类的对象以获得布尔类型值,该值将确定第一个对象是否具有较大的" x"值。
在这种情况下,关系运算符在3大于-1时返回true。
结果,程序将打印" point1大于point2"。
python中的更多关系运算符
这是可以以相同方式重载的关系运算符的列表。
Operator | Description | Method |
> | Greater than | gt(self, other) |
>= | Greater than or equal to | ge(self, other) |
< | Less than | lt(self, other) |
<= | Less than or equal to | le(self, other) |
== | Equal to | eq(self, other) |
!= | Not equal to | ne(self, other) |