stack overflow python overload + operator

class optr_ovrld:

    def __init__(self,str1):
        self.str1= str1
        self.s1 = 0
        self.s2 = 0

    def __add__(self,other):

        res = ""
    
        i = 0
        while (i < len(self.str1)) or (i < len(other.str1)):
            
            if (i < len(self.str1)):
                res += self.str1[i]
    
            if (i < len(other.str1)):
                res += other.str1[i]
                
            i += 1
        return res
    def __lt__(self,other):
        i = 0
        while(i<len(self.str1)):
               self.s1 += ord(self.str1[i])
               
               i += 1
        i = 0
        while(i<len(other.str1)):
               self.s2 += ord(other.str1[i])
              
               i += 1  
        if self.s1 < self.s2:
            return True
        else:
            return False     
    def __le__(self,other):
        i = 0
        while(i<len(self.str1)):
               self.s1 += ord(self.str1[i])
               
               i += 1
        i = 0
        while(i<len(other.str1)):
               self.s2 += ord(other.str1[i])
               
               i += 1  
        if self.s1 <= self.s2:
            return True
        else:
            return False 
    def __eq__(self,other):
        i = 0
        while(i<len(self.str1)):
               self.s1 += ord(self.str1[i])
               
               i += 1
        i = 0
        while(i<len(other.str1)):
               self.s2 += ord(other.str1[i])
               
               i += 1  
        if self.s1 == self.s2:
            return True
        else:
            return False 
    def __gt__(self,other):
        i = 0
        while(i<len(self.str1)):
               self.s1 += ord(self.str1[i])
               
               i += 1
        i = 0
        while(i<len(other.str1)):
               self.s2 += ord(other.str1[i])
               
               i += 1  
        if self.s1 > self.s2:
            return True
        else:
            return False 
    def __ge__(self,other):
        i = 0
        while(i<len(self.str1)):
               self.s1 += ord(self.str1[i])
               
               i += 1
        i = 0
        while(i<len(other.str1)):
               self.s2 += ord(other.str1[i])
               
               i += 1  
        if self.s1 >= self.s2:
            return True
        else:
            return False 

obj1 = optr_ovrld(input("enter 1st string :: "))
obj2 = optr_ovrld(input("enter 2nd string :: "))
print(obj1 + obj2)
print(obj1 < obj2)
print(obj1 > obj2)
print(obj1 <= obj2)
print(obj1 >= obj2)
print(obj1 == obj2)

4.17
6
Awgiedawgie 440220 points

                                    &gt;&gt;&gt; class MyClass(object):
...     def __add__(self, x):
...         return '%s plus %s' % (self, x)
... 
&gt;&gt;&gt; obj = MyClass()
&gt;&gt;&gt; obj + 1
'&lt;__main__.MyClass object at 0xb77eff2c&gt; plus 1'

4.17 (6 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source