Why I don't get the expected output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why I don't get the expected output?

class SpecialString: def __init__(self, cont): self.cont = cont def __truediv__(self, other): line = "=" * len(other.cont) return SpecialString (self.cont+ line+ other.cont) spam = SpecialString("spam") hello = SpecialString("Hello world!") print(spam / hello)

13th Feb 2018, 4:52 PM
Azazul Islam
Azazul Islam - avatar
4 Answers
+ 4
Because print is doing what it normally does with classes. Display the type and address. This would display the string, assuming that is what you expected. print((spam / hello).cont)
13th Feb 2018, 5:08 PM
John Wells
John Wells - avatar
+ 3
You could also do: def __str__(self): return self.cont
13th Feb 2018, 5:11 PM
John Wells
John Wells - avatar
+ 3
You are defining a method that returns a string contents of the class. print looks for one of those prior to performing it's default action.
13th Feb 2018, 5:15 PM
John Wells
John Wells - avatar
+ 2
I get the first answer; but what about the last??(btw thanks a lot)
13th Feb 2018, 5:13 PM
Azazul Islam
Azazul Islam - avatar