+ 1

why not "spam" refers to self.other and "Hello world!" refers to self.cont

as both spam and hello callings are having difference only in attributes why "spam" refers to self.cont while "Hello world!" refers to self.other istead of "spam" to self.other and " hello world " to self.cont mainly while calling specialstring class same to both spam and hello. hello should first call cont why it is going to other totally iam getting full confusion with this code please can any one help me to understand this code step by step class SpecialString: def __init__(self, cont): self.cont = cont def __truediv__(self, other): line = "=" * len(other.cont) return "\n".join([self.cont, line, other.cont]) spam = SpecialString("spam") hello = SpecialString("Hello world!") print(spam / hello)

11th Jun 2019, 11:44 AM
Raj kumar Darshanala
6 Respostas
+ 2
Exactly. Btw, it's just a convention to refer to them as "self" and "other". You could call them whatever you want, but "self" and "other" are usually used because it makes obvious what your code is doing
11th Jun 2019, 12:11 PM
Anna
Anna - avatar
+ 3
spam and hello are instances of the SpecialString class. spam / hello calls the __truediv__ method. It's the same as calling SpecialString.__truediv__(spam, hello). So "spam" refers to "self" and "hello" refers to "other". There is no "self.other". self and other are two different instances of the same class. Please upload your code to the code playground next time.
11th Jun 2019, 11:57 AM
Anna
Anna - avatar
+ 3
No inheritance. You could change it to class SpecialString(str) to make it inherit from the string class. That would allow you to use string slicing on SpecialString objects etc.
11th Jun 2019, 12:08 PM
Anna
Anna - avatar
+ 2
now i got other is also a key word for instance right? and cont is attribute
11th Jun 2019, 12:07 PM
Raj kumar Darshanala
+ 2
yeah i got it thanq very much
11th Jun 2019, 12:12 PM
Raj kumar Darshanala
+ 1
here we are using inheritance also. right?
11th Jun 2019, 12:03 PM
Raj kumar Darshanala