- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
Python 的标准运算符列表包括 __add__(a, b)
和 __concat__(a, b)
。它们通常都由 a + b
调用。我的问题是,它们之间有什么区别?是否存在使用一种而不使用另一种的情况?您是否有任何理由在单个对象上定义两者?
这是 documentation我找到了中提到的方法。
编辑:更奇怪的是这个 documentation :
Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods
__add__()
,__radd__()
,__iadd__()
,__mul__()
,__rmul__()
and__imul__()
described below; they should not define__coerce__()
or other numerical operators.
最佳答案
如果您检查 operator
模块(add、concat)的源代码,您会发现这些函数的定义如下:
def add(a, b):
"Same as a + b."
return a + b
def concat(a, b):
"Same as a + b, for a and b sequences."
if not hasattr(a, '__getitem__'):
msg = "'%s' object can't be concatenated" % type(a).__name__
raise TypeError(msg)
return a + b
所以除了concat
实际上需要一个序列类型之外,实际上没有区别。这两个函数都使用 +
运算符,其效果取决于您添加的类型。
一般来说,使用operator
module大多数时候没那么有用。当您需要传递一个执行操作的函数时,该模块主要被使用,例如像 map
这样的功能函数。 , filter
, 或 reduce
.但通常情况下,您可以直接使用 +
运算符。
至于下划线函数(__add__
和__concat__
),这些是just aliases :
__add__ = add
__concat__ = concat
但那些当然与special methods无关用于重载自定义类型的运算符。它们是与那些特殊方法匹配相同名称的函数,可能是为了使它们看起来相似。请注意,对象上没有特殊的 __concat__
方法。
然而,在自定义类型上实现 __add__
会影响运算符模块函数的工作方式,例如:
>>> class Example:
def __init__ (self, x):
self.x = x
def __repr__ (self):
return 'Example({})'.format(self.x)
def __add__ (self, other):
return Example(self.x + other.x)
>>> a = Example(2)
>>> b = Example(4)
>>> operator.add(a, b)
Example(6)
>>> a + b
Example(6)
如您所见,operator.add
将使用特殊方法Example.__add__
的实现;但这样做的原因是 operator.add
的实现只使用了 +
运算符(这种行为是由特殊的 __add__
方法明确定义的).
关于python - Python 的 __add__ 和 __concat__ 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924533/
class C(object): def __init__(self, value): self.value = value def __add__(self, other):
当我尝试这个时: >>> "a".__add__("b") 'ab' 它正在工作。但是这个 >>> 1.__add__(2) SyntaxError: invalid syntax 不工作。这是有效的
如果 python 是一种支持重载的静态编程语言,那将很容易解决。我正在制作一个名为 Complex 的类,它是复数的表示(我知道 python 有它自己的,但我想自己制作一个),其中 a 是实数,b
在 python 3 中,我想在我创建的类中进行运算符重载。 我知道__add__代表运算符+。 -的方法是什么? , * , ^ , |和 & ? 最佳答案 参见 the python docs o
我想合并定义了 __add__ 方法的类的类实例列表。 即,我有一个类实例列表L=[A,B,C,D],我想要它们的总和E = A+B+C+D,但一般化后,我可以使用 E = sum(L) 代替 +
我一直在通过搜索教程来学习 Python 2。我正在发现关于类的事情。我在 __add__ dunder-method 中遇到了一些麻烦。我不知道如何添加 一个类的 2 个对象。 为了更清楚: cla
我希望实现如下内容。有一个类foo其中包含 infoo 的列表对象。我在想,为了重载 __add__运营商我应该 class foo(object): def __init__(self, i
我正在编写一个解析器,能够将字符串解析为方程。 当我遇到一个符号(例如 +)时,我需要生成一个接受两个输入并将它们相加的函数。 虽然我可以简单地编写一个 lambda 函数来做到这一点,但为这样的目的
我正在为我自己的解释器开发一个简单的类型系统。我正在写这样的东西: class Base(object): def __init__(self, content): self.
我正在阅读学习 Python 第 5 版,我需要对这一段进行更多解释: The __add__ method of strings, for example, is what really perfo
在 Python 中,我可以重载对象的 __add__ 方法(或其他双下划线又名“dunder”方法)。这允许我在使用 Python 运算符时为我的对象定义自定义行为。 是否有可能从 dunder 方
我需要编写一个涉及日期的类。我应该重载 +运算符允许将天数添加到日期中。解释它是如何工作的:A Date对象以 (year, month, date) 的格式表示为 (2016, 4, 15)。将整数
我目前正在学习 python 运算符重载(准确地说是 __radd__ 和 __add__),我有以下代码 class Commuter1: def __init__(self, val):
我正在(作为练习)尝试制作一个使用罗马数字的类(class)。我试图将尽可能多的功能放入其中,所以我考虑让运算符(operator)在它们上工作。我发现(例如,问题在其他方法对中仍然存在)__add_
我有一个类,其中包含一些数据。此类的两个对象的运算符 + 应该合并来自两个对象的数据并返回一个新对象。但是,如果在 += 运算符的情况下从添加的对象附加数据并返回 self 应该会好得多。这里有一些伪
class Matrix: def __init__(self, data): self.data = data def __repr__(self): return repr
我一直认为在 Python (3.5) 中使用“+”运算符会在后台调用 __add__ 方法并返回总和。但是,当涉及负数时,我注意到一些古怪的行为。当然, >>>-3 + 7 返回 4 但是(!) >
在 Python 中,add 和 __add__ 方法有什么区别? 最佳答案 名为 add 的方法就是那个 - 具有该名称的方法。它对语言或解释器没有任何特殊意义。关于它唯一可以说的是集合有一个同名的
当将整数值添加到浮点值时,我意识到如果在浮点上调用 __add__ 方法工作正常,例如: >>> n = 2.0 >>> m = 1 >>> n.__add__(m) 3.0 但如果对整数调用则不是:
我有一个矢量类: class Vector: def __init__(self, x, y): self.x, self.y = x, y def __str__(s
我是一名优秀的程序员,十分优秀!