+ 4

How does this function work?

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 I don't understand from 5th line(for index...)

2nd Sep 2020, 7:32 AM
Sadat Rahman
Sadat Rahman - avatar
7 ответов
+ 2
Oh it is a complete missuse of the gt operator. Ok pencil and paper and step by step Let index be 0 Result = egg[0:0]+ ">" + "spam" what leads to >spam next line is Result+=">"+eggs[0:] Now result is >spam>eggs try yourself with next index
2nd Sep 2020, 7:51 AM
Oma Falk
Oma Falk - avatar
+ 2
Oma Folk & Théophile. thanks
2nd Sep 2020, 8:22 AM
Sadat Rahman
Sadat Rahman - avatar
+ 2
Théophile why is Sadat Rahman first one who asks? 🤔
3rd Sep 2020, 7:01 AM
Oma Falk
Oma Falk - avatar
+ 2
why does OOP seem hard to everyone
3rd Sep 2020, 9:37 AM
Sadat Rahman
Sadat Rahman - avatar
+ 1
Théophile can't remember having seen it in the lessons.
2nd Sep 2020, 8:41 AM
Oma Falk
Oma Falk - avatar
0
Oma Falk Is it really an example from the SL Python course? Wow... It is a very bad use of __gt__, as you said. It is teaching bad practices...
2nd Sep 2020, 7:59 AM
Théophile
Théophile - avatar
0
Oma Falk I just found it inside the 'magic methods' part (3rd example) . https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2470/
2nd Sep 2020, 8:55 AM
Théophile
Théophile - avatar