I have a class, which contains some data. Operator + for two objects of such class should merge data from both objects and returns a new object. However, it should be much better if in the case of += operator would append data from the added object and return self. Here some pseudo code to demonstrate what I want to achieve.
class BiGData:
def __init__(self):
self.data=[]
def __add__(self,x):
if (this is just a +):
newData=BigData()
newData.data += self.data
newData.data += x.data
return newData
else: #(this is the += case)
self.data += x.data
return self
If these two case of usage __add__
function can be distinguished, Python code would much better read and understand! Consider this:
x,y=BigData(),BigData()
z = x + y # z is a new object of the BigData class,
# which collects all data from both x and y
# but data in x and y are intacked
x += y # just append data from y to x
# data in x has been changed