I know, that the purpose of str()
method is to return the string representation of an object, so I wanted to test what happens if I force it to make something else.
I've created a class and an object:
class MyClass(object):
def __str__(self, a=2, b=3):
return a + b
mc = MyClass()
When I call:
print(str(mc))
The interpreter complains:
TypeError: __str__ returned non-string (type int)
And this is fully understandable because the str() method is trying to return int.
But if I try:
print(mc.__str__())
I get the output: 5.
So why the interpreter allows me to return int when I call __str__
directly, but not when I'm using str(mc) which - as I understand - is also evaluated to mc.__str__()
.