博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
运算符重载 python_Python运算符重载
阅读量:2509 次
发布时间:2019-05-11

本文共 3677 字,大约阅读时间需要 12 分钟。

运算符重载 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 = 10b = 20print (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:

可以重载的运算符如下:

Operators Methods
+ __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 overloadingclass 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 overloadingclass 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中实现运算符重载。 如果您有任何疑问,请在下面评论。

翻译自:

运算符重载 python

转载地址:http://xoggb.baihongyu.com/

你可能感兴趣的文章
ios上架报错90080,90087,90209,90125 解决办法
查看>>
给button添加UAC的小盾牌图标
查看>>
如何退出 vim
查看>>
Robberies
查看>>
get post 提交
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>
20121016学习笔记四
查看>>
EntityFramework 学习 一 Stored Procedure
查看>>
Sliverlight之 故事板
查看>>
Java 必知必会的 20 种常用类库和 API
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>
0007_初始模块和字节码
查看>>
[效率提升]如何管理好你的电脑文件
查看>>
C++实验二
查看>>
SharePoint2010 富文本框添加图片功能的扩展
查看>>
零零碎碎的知识
查看>>
UNIX基础--用户和基本账户管理
查看>>
设计模式
查看>>
5.0以上机器XPOSED框架安装流程
查看>>
静态方法与非静态方法
查看>>