Answers for "stack overflow python overload operator"

0

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)
Posted by: Guest on June-15-2021
0

operator overloading python stack overflow

>>> class MyClass(object):
...     def __add__(self, x):
...         return '%s plus %s' % (self, x)
... 
>>> obj = MyClass()
>>> obj + 1
'<__main__.MyClass object at 0xb77eff2c> plus 1'
Posted by: Guest on October-08-2020
0

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)
Posted by: Guest on June-15-2021
0

operator overloading python stack overflow

>>> class MyClass(object):
...     def __add__(self, x):
...         return '%s plus %s' % (self, x)
... 
>>> obj = MyClass()
>>> obj + 1
'<__main__.MyClass object at 0xb77eff2c> plus 1'
Posted by: Guest on October-08-2020

Python Answers by Framework

Browse Popular Code Answers by Language