0
Please explain to me, why in the following code, in the first line of output, there is a sign ">" standing before "spam"?
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 Output: >>> >spam>eggs e>spam>ggs eg>spam>gs egg>spam>s eggs>spam> >>>
2 Respostas
+ 2
Because the first iteration other.cont[:index] returns an empty string (given that index=0).
+ 1
Now I got it. It is obvious. Sometimes something goes wrong in our brains :).