add more than two objects with __add__ function
Question:
I have a class it can successfully add two variables of object of class a
class a():
def __add__(self, other):
return self.val+other.val
def __init__(self,a):
self.val=a
aa=a(22)
bb=a(11)
aa+bb
33
But when I try to give it third object to add, it through error
cc=a(11)
aa+bb+cc
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
aa+bb+cc
TypeError: unsupported operand type(s) for +: 'int' and 'a'
It is because first two aa+bb return int and its add function is design to add object addition
Any one can suggest how I can add three objects
I find this link Using __add__ operator with multiple arguments in Python but it is working on one object and 2 integers. But I want to add three objects. and all these three combine and return integer
Answers:
__add__
must return an instance of a
class, not int
class a():
def __add__(self, other):
return a(self.val + other.val)
def __init__(self, a):
self.val = a
Here’s an example of how __add__ and __radd__ should be implemented.
We have a class A that has an attribute n which is an integer. We want to be able to add classes of the same type and we also want to be able to add integer values. Therefore:
class A:
def __init__(self, n):
self._n = n
@property
def n(self):
return self._n
def __add__ (self, other):
if isinstance(other, int):
return A(self.n + other)
assert isinstance(other, type(self))
return A(self.n + other.n)
def __radd__(self, other):
assert isinstance(other, int)
return A(self.n + other)
def __str__(self):
return f'{self.n}'
def __repr__(self):
return f'{self.n=}'
a = A(1)
b = A(2)
c = A(3)
print(10+a+10+b+10+c+10)
c += 5
print(c)
print(c.n)
Output:
46
8
8
This is what I looking for
class a():
def __add__(self, other):
return a(self.val+other.val)
def __init__(self,a):
self.val=a
aa=a(22)
bb=a(11)
cc=a(11)
d=aa+bb+cc
print(d.val)
Out put
44
I have a class it can successfully add two variables of object of class a
class a():
def __add__(self, other):
return self.val+other.val
def __init__(self,a):
self.val=a
aa=a(22)
bb=a(11)
aa+bb
33
But when I try to give it third object to add, it through error
cc=a(11)
aa+bb+cc
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
aa+bb+cc
TypeError: unsupported operand type(s) for +: 'int' and 'a'
It is because first two aa+bb return int and its add function is design to add object addition
Any one can suggest how I can add three objects
I find this link Using __add__ operator with multiple arguments in Python but it is working on one object and 2 integers. But I want to add three objects. and all these three combine and return integer
__add__
must return an instance of a
class, not int
class a():
def __add__(self, other):
return a(self.val + other.val)
def __init__(self, a):
self.val = a
Here’s an example of how __add__ and __radd__ should be implemented.
We have a class A that has an attribute n which is an integer. We want to be able to add classes of the same type and we also want to be able to add integer values. Therefore:
class A:
def __init__(self, n):
self._n = n
@property
def n(self):
return self._n
def __add__ (self, other):
if isinstance(other, int):
return A(self.n + other)
assert isinstance(other, type(self))
return A(self.n + other.n)
def __radd__(self, other):
assert isinstance(other, int)
return A(self.n + other)
def __str__(self):
return f'{self.n}'
def __repr__(self):
return f'{self.n=}'
a = A(1)
b = A(2)
c = A(3)
print(10+a+10+b+10+c+10)
c += 5
print(c)
print(c.n)
Output:
46
8
8
This is what I looking for
class a():
def __add__(self, other):
return a(self.val+other.val)
def __init__(self,a):
self.val=a
aa=a(22)
bb=a(11)
cc=a(11)
d=aa+bb+cc
print(d.val)
Out put
44