+ 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)
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)
+ 3
You could also do:
def __str__(self):
return self.cont
+ 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.
+ 2
I get the first answer; but what about the last??(btw thanks a lot)