operator overloading python stack overflow

Solutions on MaxInterview for operator overloading python stack overflow by the best coders in the world

showing results for - "operator overloading python stack overflow"
Juan Diego
08 Jan 2017
1class optr_ovrld:
2
3    def __init__(self,str1):
4        self.str1= str1
5        self.s1 = 0
6        self.s2 = 0
7
8    def __add__(self,other):
9
10        res = ""
11    
12        i = 0
13        while (i < len(self.str1)) or (i < len(other.str1)):
14            
15            if (i < len(self.str1)):
16                res += self.str1[i]
17    
18            if (i < len(other.str1)):
19                res += other.str1[i]
20                
21            i += 1
22        return res
23    def __lt__(self,other):
24        i = 0
25        while(i<len(self.str1)):
26               self.s1 += ord(self.str1[i])
27               
28               i += 1
29        i = 0
30        while(i<len(other.str1)):
31               self.s2 += ord(other.str1[i])
32              
33               i += 1  
34        if self.s1 < self.s2:
35            return True
36        else:
37            return False     
38    def __le__(self,other):
39        i = 0
40        while(i<len(self.str1)):
41               self.s1 += ord(self.str1[i])
42               
43               i += 1
44        i = 0
45        while(i<len(other.str1)):
46               self.s2 += ord(other.str1[i])
47               
48               i += 1  
49        if self.s1 <= self.s2:
50            return True
51        else:
52            return False 
53    def __eq__(self,other):
54        i = 0
55        while(i<len(self.str1)):
56               self.s1 += ord(self.str1[i])
57               
58               i += 1
59        i = 0
60        while(i<len(other.str1)):
61               self.s2 += ord(other.str1[i])
62               
63               i += 1  
64        if self.s1 == self.s2:
65            return True
66        else:
67            return False 
68    def __gt__(self,other):
69        i = 0
70        while(i<len(self.str1)):
71               self.s1 += ord(self.str1[i])
72               
73               i += 1
74        i = 0
75        while(i<len(other.str1)):
76               self.s2 += ord(other.str1[i])
77               
78               i += 1  
79        if self.s1 > self.s2:
80            return True
81        else:
82            return False 
83    def __ge__(self,other):
84        i = 0
85        while(i<len(self.str1)):
86               self.s1 += ord(self.str1[i])
87               
88               i += 1
89        i = 0
90        while(i<len(other.str1)):
91               self.s2 += ord(other.str1[i])
92               
93               i += 1  
94        if self.s1 >= self.s2:
95            return True
96        else:
97            return False 
98
99obj1 = optr_ovrld(input("enter 1st string :: "))
100obj2 = optr_ovrld(input("enter 2nd string :: "))
101print(obj1 + obj2)
102print(obj1 < obj2)
103print(obj1 > obj2)
104print(obj1 <= obj2)
105print(obj1 >= obj2)
106print(obj1 == obj2)
107
Paolo
21 Jul 2018
1>>> class MyClass(object):
2...     def __add__(self, x):
3...         return '%s plus %s' % (self, x)
4... 
5>>> obj = MyClass()
6>>> obj + 1
7'<__main__.MyClass object at 0xb77eff2c> plus 1'