在 Python 中使用带有多个参数的 __add__ 运算符
Using __add__ operator with multiple arguments in Python
我正在尝试添加带有数字的 class 对象,但我对如何添加带有两个数字的 class 对象感到困惑。例如,这是我假设的 add class 方法:
class A:
def __add__(self, b):
return something
到目前为止我知道如何添加:
a = A()
print(a + 1)
但是,如果我想这样添加呢?
a = A()
print(a + 1 + 2)
我应该为 add class 方法使用 *args 吗?
不,您不能使用多个参数。 Python 分别执行每个 +
运算符,两个 +
运算符是不同的表达式。
对于您的示例,object + 1 + 2
确实是 (object + 1) + 2
。如果 (object + 1)
生成具有 __add__
方法的对象,则 Python 将为第二个运算符调用该方法。
例如,您可以在此处 return A
的另一个实例:
>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>
即使有多个值,它也能完美工作,因为每次添加只添加两个值(当您添加多个值时,请参见多个 + 号):
class A:
def __init__(self, value):
self.a = value
def __add__(self, another_value):
return self.a + another_value
a = A(1)
print(a+1+1)
你会希望你的 return 值是一个对象本身,它也支持添加操作,例如:
class A:
def __init__(self, value=0):
self.value = value
def __add__(self, b):
return A(self.value + b)
def __str__(self):
return str(self.value)
a = A()
print(a + 1 + 2)
输出:
3
你总是可以这样做:
>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>
我正在尝试添加带有数字的 class 对象,但我对如何添加带有两个数字的 class 对象感到困惑。例如,这是我假设的 add class 方法:
class A:
def __add__(self, b):
return something
到目前为止我知道如何添加:
a = A()
print(a + 1)
但是,如果我想这样添加呢?
a = A()
print(a + 1 + 2)
我应该为 add class 方法使用 *args 吗?
不,您不能使用多个参数。 Python 分别执行每个 +
运算符,两个 +
运算符是不同的表达式。
对于您的示例,object + 1 + 2
确实是 (object + 1) + 2
。如果 (object + 1)
生成具有 __add__
方法的对象,则 Python 将为第二个运算符调用该方法。
例如,您可以在此处 return A
的另一个实例:
>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>
即使有多个值,它也能完美工作,因为每次添加只添加两个值(当您添加多个值时,请参见多个 + 号):
class A:
def __init__(self, value):
self.a = value
def __add__(self, another_value):
return self.a + another_value
a = A(1)
print(a+1+1)
你会希望你的 return 值是一个对象本身,它也支持添加操作,例如:
class A:
def __init__(self, value=0):
self.value = value
def __add__(self, b):
return A(self.value + b)
def __str__(self):
return str(self.value)
a = A()
print(a + 1 + 2)
输出:
3
你总是可以这样做:
>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>>>> class A:
... def __init__(self, val):
... self.val = val
... def __repr__(self):
... return f'<A({self.val})>'
... def __add__(self, other):
... print(f'Summing {self} + {other}')
... return A(self.val + other)
...
>>> A(42) + 10
Summing A(42) + 10
<A(52)>
>>> A(42) + 10 + 100
Summing A(42) + 10
Summing A(52) + 100
<A(152)>