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
javatiku
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
:  javatiku

程序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中重载相等和小于运算符

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)

比较运算符:

运算符魔术函数
<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)

一元运算符:

运算符魔术函数
-NEG(SELF, OTHER)
+POS(SELF, OTHER)
~INVERT(SELF, OTHER)

结论

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

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程