Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 unexpected sub result after overloading operator
#1
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))
Quote
#2
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)):
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unexpected expected type error result MartinMaker 1 297 Feb-16-2019, 05:02 PM
Last Post: micseydel
  Unexpected result eftimios 1 417 Dec-02-2018, 07:39 AM
Last Post: Gribouillis
  Unexpected result in simple prime number example jackhj 2 697 Apr-20-2018, 01:48 AM
Last Post: jackhj
  Program not running (Overloading problem) anurag123 2 728 Feb-19-2018, 07:23 PM
Last Post: nilamo
  Overloading error PyMan 1 537 Feb-19-2018, 04:57 AM
Last Post: metulburr
  Reversing word in strings yields unexpected result Dec 4 1,261 May-17-2017, 05:32 PM
Last Post: wavic
  datetime unexpected result PickyBiker 10 3,425 Dec-27-2016, 10:47 PM
Last Post: PickyBiker

Forum Jump:


Users browsing this thread: 1 Guest(s)