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
  remove tags from BeautifulSoup result moski 1 104 Jun-05-2019, 01:47 PM
Last Post: heiner55
  AND Boolean operator syntax error jsb83 3 222 May-31-2019, 08:34 AM
Last Post: perfringo
  call dict object result key error lateublegende 2 134 May-15-2019, 01:08 PM
Last Post: lateublegende
  syntax error near unexpected token btom529 5 267 Apr-21-2019, 09:03 PM
Last Post: btom529
  Getting an unexpected generator object ? mcgrim 19 424 Mar-29-2019, 11:35 AM
Last Post: gontajones
  Force calculation result as decimal vercetty92 4 207 Mar-20-2019, 02:27 PM
Last Post: vercetty92
  Dictionary adds an unexpected list erina 1 189 Mar-14-2019, 01:13 PM
Last Post: ichabod801
  unexpected EOF while parsing whatloop 3 412 Mar-09-2019, 06:59 PM
Last Post: whatloop
  test pattern and add result in a table sam1975 1 159 Mar-05-2019, 02:41 PM
Last Post: sam1975
  SyntaxError: can't assign to operator Jomy10 3 675 Mar-02-2019, 12:39 PM
Last Post: Jomy10

Forum Jump:


Users browsing this thread: 1 Guest(s)