Python Forum
unexpected sub result after overloading operator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unexpected sub result after overloading operator
#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)):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: unexpected sub result after overloading operator - by buran - Jan-24-2019, 08:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  operator overloading won't work MaartenRo 1 1,782 Aug-04-2020, 02:37 AM
Last Post: deanhystad
  Operator Overloading issue mp3909 4 2,237 Jun-07-2020, 06:34 PM
Last Post: buran
  Unexpected result linton 4 1,939 May-02-2020, 01:15 PM
Last Post: linton
  list sum gives unexpected result Nesso 0 1,668 Feb-04-2020, 08:31 AM
Last Post: Nesso
  Unexpected (?) result with regular expressions guraknugen 2 2,164 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Unexpected expected type error result MartinMaker 1 2,017 Feb-16-2019, 05:02 PM
Last Post: micseydel
  Unexpected result eftimios 1 2,535 Dec-02-2018, 07:39 AM
Last Post: Gribouillis
  Unexpected result in simple prime number example jackhj 2 2,954 Apr-20-2018, 01:48 AM
Last Post: jackhj
  Program not running (Overloading problem) anurag123 2 2,522 Feb-19-2018, 07:23 PM
Last Post: nilamo
  Overloading error PyMan 1 2,285 Feb-19-2018, 04:57 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020