8.4k views
0 votes
Write a class Example() such that it has a method that gives the difference between the size of strings when the '-' (subtraction) symbol is used between the two objects of the class. Additionally, implement a method that returns True if object 1 is greater than object 2 and False otherwise when the (>) (greater than) symbol is used. For example: obj1

User Ruseel
by
3.7k points

1 Answer

4 votes

Answer:

Here the code is given as follows,

Step-by-step explanation:

class Example:

def _init_(self, val):

self.val = val

def _gt_(self, other):

return self.val > other.val

def _sub_(self,other):

return abs(len(self.val) - len(other.val))

def main():

obj1 = Example('this is a string')

obj2 = Example('this is another one')

print(obj1 > obj2)

print(obj1 - obj2)

main()

Output:-

Write a class Example() such that it has a method that gives the difference between-example-1
User Matt Berkowitz
by
3.7k points