Ask Question
16 July, 06:41

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 = Example ('this is a string') obj2 = Example ('this is another one') print (obj1 > obj2) False print (obj1-obj2) 3

+5
Answers (1)
  1. 16 July, 09:37
    0
    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 ()

    /color{red}/underline{Output:}
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a class Example () such that it has a method that gives the difference between the size of strings when the '-' (subtraction) symbol ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers