Python Forum
unexpected sub result after overloading operator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unexpected sub result after overloading operator (/thread-15604.html)



unexpected sub result after overloading operator - jolinchewjb - Jan-24-2019

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))



RE: unexpected sub result after overloading operator - buran - Jan-24-2019

I have added a print in the __init__(), so you can see what is going on:

class Opera(object): 
    def __init__(self,opera):
        self.opera=[]
        print('print from __init__()', type(opera), type(opera) in (list,tuple))
        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))
Output:
print from __init__() <class 'list'> True print from __init__() <class 'list'> True print from __init__() <class 'list'> True p1:Operator overload Example['red', 'Green', 'Orange'] p2:Operator overload Example['go', 'stop'] p3:Operator overload Example['Orange'] print from __init__() <class 'list'> True p1+p2:Operator overload Example['red', 'Green', 'Orange', 'go', 'stop'] print from __init__() <class 'generator'> False p1-p3:Operator overload Example[] >>>
As you can see (second line from the end) p for p in self.opera if p not in other.opera on line 11 is a generator expression, it's not neither list nor tuple, so your if condition in the __init__() is evaluated False.

That said the recommended way to test if an object is instance of certain class/type is
if isinstance(opera, (list,tuple)):