运算符重载 python_Python运算符重载

运算符重载 python

In this article, you’ll learn about python operator overloading with examples.

在本文中,您将通过示例了解python运算符重载。

We all know what are operators (+, -, <=). In python, operators work for built in classes, but some operator behaves differently with different types. For example ‘+’ operator can add two numbers and also can concatenate two strings.

我们都知道什么是运算符(+,-,<=)。 在python中,运算符为内置类工作,但是某些运算符在不同类型上的行为有所不同。 例如,“ +”运算符可以添加两个数字,也可以连接两个字符串。

Program:

程序:

a = 10
b = 20
print (a+b)

Output:

输出:

30

30

a = "hello "
b = "programmer"
print(a+b)

Output:

输出:

hello programmer”

你好程序员”

So, using any operator to perform different operation that are not usually performed, is known as operator overloading. We can change the behavior of operators using operator overloading.

因此,使用任何运算符执行通常不执行的不同操作,称为运算符重载。 我们可以使用运算符重载来更改运算符的行为。

Or we can also say that “assigning a different work to an operator is known as operator overloading”.

或者我们也可以说“将不同的工作分配给操作员称为操作员超载”。

To perform operator overloading, there are some magic methods provided by Python. Using these methods we can perform any operation we want on a operator.

为了执行运算符重载,Python提供了一些魔术方法。 使用这些方法,我们可以对运算符执行所需的任何操作。

The operators that can be overloaded are as follows:

可以重载的运算符如下:

OperatorsMethods
+__add__(self, other)
__sub__(self, other)
*__mul__(self, other)
//__floordiv__(self, other)
/__div__(self, other)
%__mod__(self, other)
**__pow__(self, other[ , modulo])
<__lt__(self, other)
<=__le__(self, other)
==__eq__(self, other)
!=__ne__(self , other)
>=__ge__(self, other)
经营者 方法
+ __add __(自己,其他)
__sub __(自己,其他)
* __mul __(自己,其他)
// __floordiv __(自己,其他)
/ __div __(自己,其他)
__mod __(自己,其他)
** __pow __(self,other [,modulo])
< __lt __(自己,其他)
<= __le __(自己,其他)
== __eq __(自己,其他)
!= __ne __(自己,其他)
> = __ge __(自己,其他)

The basic idea to perform the operator overloading in python is to define any of these methods in the class then call them using operators.

在python中执行运算符重载的基本思想是在类中定义任何这些方法,然后使用运算符调用它们。

Let’s see an example. Suppose we want to overload  ‘+’ operator.

让我们来看一个例子。 假设我们要重载“ +”运算符。

As mentioned above that we can concatenate two strings and add two numbers with the help of ‘+’ operator but here we’ll perform the addition on two objects of a class named as Test.

如上所述,我们可以连接两个字符串并借助'+'运算符将两个数字相加,但是这里我们将对名为Test的类的两个对象执行相加。

Program:

程序:

#without  operator overloading
class Test:  
  def __init__(self, value):
    self.value = value
 
n1 = Test(20)
n2 = Test(10)
print (n1 + n2)

Output:

输出:

TypeError: unsupported operand type(s) for +: ‘Test’ and ‘Test’

TypeError:+不支持的操作数类型:“测试”和“测试”

So there is an error that says that we can’t perform addition to add values of the both the Test class’s objects. So we will define the __add___(self, other)  method  here to add the values of both the objects.

因此,出现一个错误,表明我们无法执行加法运算来添加两个Test类的对象的值。 因此,我们将在此处定义__add ___(self,other)方法以添加两个对象的值。

Program:

程序:

#with operator overloading
class Test:
  def __init__(self, value):
    self.value = value
   
  def  __add__(self, other):
    return (self.value + other.value)
   
n1 = Test(5)
n2 = Test(6)
print (n1 + n2)

Output:

输出:

11

11

So what we did in above program is just added a __add__(self, other) method that will be called when we use ‘+’ operator on the objects of the Test class.

因此,我们在上述程序中所做的只是添加了一个__add __(self,other)方法,当我们在Test类的对象上使用'+'运算符时将调用该方法。

It is not necessary to always perform addition on ‘+’ operator we can do anything we want like we can also subtract object’s values by using operator ‘+’.

不必总是对'+'运算符执行加法,我们可以做任何我们想做的事情,就像我们也可以通过使用运算符'+'减去对象的值一样。

In this way you can achieve operator overloading in python. Comment below if you have any doubts.

这样,您可以在python中实现运算符重载。 如果您有任何疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/02/python-operator-overloading.html

运算符重载 python

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值