Python中的运算符重载是什么意思

Python中的运算符重载是什么意思

在Python中,运算符重载意味着提供超出预定义操作意义的扩展含义。例如,我们可以使用“+”运算符来添加两个整数,也可以用来连接两个字符串或合并两个列表。我们可以通过“int”类和“str”类重载“+”运算符来实现这一点。用户可以注意到相同的内置运算符或函数对于不同类的对象显示出不同的行为。这个过程被称为运算符重载。

示例:

print (14 + 32)

# Now, we will concatenate the two strings
print ("Java" + "Tpoint")

# We will check the product of two numbers
print (23 * 14)

# Here, we will try to repeat the String
print ("X Y Z " * 3)

输出:

46
JavaTpoint
322
X Y Z X Y Z X Y Z

如何在Python中重载运算符

假设用户有两个对象,它们是用户自定义数据类型类的物理表示。用户必须使用”+”运算符来添加这两个对象,但是却出现了错误。这是因为编译器不知道如何添加这两个对象。因此,用户必须定义使用该运算符的函数,这个过程被称为”运算符重载”。用户可以重载所有现有的运算符,但不能创建任何新的运算符。Python提供了一些特殊的函数,或者我们可以称之为魔术函数,用于执行运算符重载,当它与特定的运算符关联时,这些函数会自动被调用。例如,当用户使用”+”运算符时,魔术函数add会自动在定义”+”运算符的命令中被调用。

如何在Python中执行二进制”+”运算符:

当用户在用户自定义的类的数据类型上使用运算符时,与该运算符关联的魔术函数会自动被调用。更改运算符的行为过程就像定义函数或方法的行为一样简单。

用户在类中定义方法或函数,运算符根据这些函数中定义的行为工作。当用户使用”+”运算符时,它将改变魔术函数的代码,使得”+”运算符有额外的含义。

程序1:简单地添加两个对象

用于简单地使用重载运算符来添加两个对象的Python程序。

示例:

class example:
    def __init__(self, X):
        self.X = X

    # adding two objects
    def __add__(self, U):
        return self.X + U.X
object_1 = example( int( input( print ("Please enter the value: "))))
object_2 = example( int( input( print ("Please enter the value: "))))
print (": ", object_1 + object_2)
object_3 = example(str( input( print ("Please enter the value: "))))
object_4 = example(str( input( print ("Please enter the value: "))))
print (": ", object_3 + object_4) 

输出:

Please enter the value: 23
Please enter the value: 21
:  44
Please enter the value: Java
Please enter the value: Tpoint
:  JavaTpoint

程序2:在另一个对象中定义重载运算符

Python程序用于在另一个对象内定义重载运算符。

示例:

class complex_1:
    def __init__(self, X, Y):
        self.X = X
        self.Y = Y

    # Now, we will add the two objects
    def __add__(self, U):
        return self.X + U.X, self.Y + U.Y

Object_1 = complex_1(23, 12)
Object_2 = complex_1(21, 22)
Object_3 = Object_1 + Object_2
print (Object_3)

输出:

(44, 34)

程序3:在Python中重载比较运算符

用于重载比较运算符的Python程序。

示例:

class example_1:
    def __init__(self, X):
        self.X = X
    def __gt__(self, U):
        if(self.X > U.X):
            return True
        else:
            return False
object_1 = example_1(int( input( print ("Please enter the value: "))))
object_2 = example_1(int (input( print("Please enter the value: "))))
if(object_1 > object_2):
    print ("The object_1 is greater than object_2")
else:
    print ("The object_2 is greater than object_1")

输出:

案例1:

Please enter the value: 23
Please enter the value: 12
The object_1 is greater than object_2

情况2:

Please enter the value: 20
Please enter the value: 31
The object_2 is greater than object_1

程序4:重载等于和小于运算符

Python程序,用于重载等于和小于运算符:

示例:

class E_1:
    def __init__(self, X):
        self.X = X
    def __lt__(self, U):
        if(self.X < U.X):
            return "object_1 is less than object_2"
        else:
            return "object_2 is less than object_1"
    def __eq__(self, U):
        if(self.X == U.X):
            return "Both the objects are equal"
        else:
            return "Objects are not equal"

object_1 = E_1(int( input( print ("Please enter the value: "))))
object_2 = E_1(int( input( print ("Please enter the value: "))))
print (": ", object_1 < object_2)

object_3 = E_1(int( input( print ("Please enter the value: "))))
object_4 = E_1(int( input( print ("Please enter the value: "))))
print (": ", object_3 == object_4)

输出:

情况1:

Please enter the value: 12
Please enter the value: 23
:  object_1 is less than object_2
Please enter the value: 2
Please enter the value: 2
:  Both the objects are equal

情况2:

Please enter the value: 26
Please enter the value: 3
: object_2 is less than object_1
Please enter the value: 2
Please enter the value: 5
: Objects are not equal

用于运算符重载的Python魔术函数:

二元运算符:

操作符 魔法函数
+ __add__(self, other)
- __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)

比较运算符:

Operator Magic Function
< __LT__(SELF, OTHER)
> __GT__(SELF, OTHER)
<= __LE__(SELF, OTHER)
>= __GE__(SELF, OTHER)
== __EQ__(SELF, OTHER)
!= __NE__(SELF, OTHER)

赋值运算符:

操作符 魔术函数
-= __ISUB__(SELF, OTHER)
+= __IADD__(SELF, OTHER)
*= __IMUL__(SELF, OTHER)
/= __IDIV__(SELF, OTHER)
//= __IFLOORDIV__(SELF, OTHER)
%= __IMOD__(SELF, OTHER)
**= __IPOW__(SELF, OTHER)
>>= __IRSHIFT__(SELF, OTHER)
<<= __ILSHIFT__(SELF, OTHER)
&= __IAND__(SELF, OTHER)
|= __IOR__(SELF, OTHER)
^= __IXOR__(SELF, OTHER)

一元运算符:

Operator Magic Function
- __NEG__(SELF, OTHER)
+ __POS__(SELF, OTHER)
~ __INVERT__(SELF, OTHER)

结论

在本教程中,我们讨论了Python中的运算符重载以及如何使用它们来执行各种操作符。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程