Jan-24-2019, 07:57 AM
I have below code to test overload of the operator, add is tested ok, sub is not ok, after P1-p3, it becomes empty set, but my sub function is correct, anyone knows why? thanks
class Opera(object):
def __init__(self,opera):
self.opera=[]
if type(opera) in (list,tuple):
self.opera.extend(opera)
def __add__(self, other):
return Opera(self.opera+other.opera)
def __sub__(self, other):
return Opera(p for p in self.opera if p not in other.opera)
def __str__(self):
return 'Operator overload Example{}'.format(self.opera)
p1=Opera(['red','Green','Orange'])
p2=Opera(['go','stop'])
p3=Opera(['Orange'])
print ('p1:'+str(p1))
print ('p2:'+str(p2))
print ('p3:'+str(p3))
print ('p1+p2:'+str(p1+p2))
print ('p1-p3:'+str(p1-p3))
