can someone help.. what does this code means so it could have the output like on the example.. class SpecialString: def __init__(self, cont): self.cont = cont def __gt__(self, other): for index in range(len(other.cont)+1): result = other.cont[:index] + ">" + self.cont result += ">" + other.cont[index:] print(result) spam = SpecialString("spam") eggs = SpecialString("eggs") spam > eggs
Please someone help..
Section #1 Well after looking at that topic right now, I think that the examples for the the majority of the magic methods could have been better made. It looks unnecessarily complicated and makes people think overriding operators is a difficult task. I will explain how the example's override code works but you don't need to know how it works in order to understand how to override operators. Two objects of class specialstring are created called spam and eggs. The class has the magic method named __gt__ to override the functionality of the greater than operator. Therefore the expression spam > eggs now means spam.__gt__(eggs). As you can see, it calls the method for the object spam with eggs as its argument. Take note of this. The __gt__ method takes the parameter other as well as self. In this case, other will be eggs since that was the argument passed in spam.__gt__(eggs). Self will be the spam object since we are calling its method. At this point, that would be basically all you needed to know about overriding operators since the code to override depends on what you need it to do. However let's continue with the essay shall we. Skip to the conclusion if you aren't too interested in example know-how.
Section #3: Conclusion To conclude, learning how to override an operator only requires you to know the name of the method you want to override an operator with, the parameters in the method that allows you to work with both objects and how you want it to work. E.g class sandwich (): def __init__(self, filling): self.filling = filling def __add__(self, other): print (self.filling," and ",other.filling, "sandwich.") Bacon = sandwich("bacon") Tuna = sandwich("tuna") Bacon+Tuna Output: bacon and tuna sandwich PS: Has anyone ever tried a bacon and tuna sandwich? Tell me how it tastes. :p
Section #2 The code the example uses in the override method prints a string every time the for loop is run. for index in range(len(other.cont)+1):// Runs the loop for the length of other.cont + 1 // cont holds the string argument passed when you made the object. For eggs it would be "eggs" from eggs = SpecialString ("eggs") //So len ("eggs")+1 = 5, which means the loop will run 5 times. Each time the loop runs it assigns an expression to variable result. What you need to know about [:index] is that it takes the character's from 0 to the index. The first time the loop runs, index is 0, so it slices the string from 0 to 0. That won't return any characters. That's why the expression other.cont[:index] + ">" + self.cont only gives '>spam' when you'd think it would include something from the string "eggs" at the beginning like it did with the other outputs. As the index increases , other.cont [:index] will return more characters to add to the string. Finally the expression in result += ">" + other.cont[index:], the [index:] will return the character's from index to the last character. So for the first loop run, index will be 0, therefore it returns all the character's of other.cont. As the index increases, the number of characters returned will decrease as the starting position of the slice gets higher. Both expressions together created the result. Therefore on the first run when index is 0: result = other.cont[:index] + ">" + self.cont result += ">" + other.cont[index:] print(result) Outputs: >spam>eggs And on the second run when index is 1: e>spam>ggs And so on....
Read from bottom up. Didn't imagine it'd be too long to fit in one post.
thank you very much for answering my question.. i hope i can learn much from what you've wrote here.. thank you :D