Why does print(t) error if t.__str__() returns a non-string, but not print(t.__str__())? Why does print(t) error if t.__str__() returns a non-string, but not print(t.__str__())? python python

Why does print(t) error if t.__str__() returns a non-string, but not print(t.__str__())?


What you’re doing is equivalent to print(5), which works because print calls __str__ on 5 to get a string. But passing the object, print calls __str__ on the object and doesn’t get an actual string in response.


It's all about extra checks that python does with some built-in functions.

len() --> __len__() + some checksstr() --> __str__() + some checks

There is a difference between when "you" call a method explicitly or when that method gets called "by Python"! The point is when Python calls your method it will do some checks for you. (That's one of the reasons that we should use those built-in functions instead of calling relevant dunder method.)

We can see that behavior with len() and __len__() as well:

class Test:    def __len__(self):        return 'foo't = Test()print(t.__len__())  # fineprint(len(t))       # TypeError: 'str' object cannot be interpreted as an integer

So python checked for returning integer in second print statement! that's what expected from __len__().

Same thing happens here. When you call print(t) Python itself calls __str__() method, so it does check to see if __str__() returns a string that is expected or not. (same thing happens with str(t))

But, when you say print(t.__str__()), first, you're calling it's __str__() method on the instance explicitly by yourself, there is no checking here... what does get back ? number 5, and then python will run print(5).


When you call t.__str__() directly it is just like any other method. The method __str__ method was overwritten, so there is nothing special about it when calling it directly.

When doing print(t) invocation happens internally, where some typechecking takes place

if (!PyUnicode_Check(res)) {    _PyErr_Format(tstate, PyExc_TypeError,                  "__str__ returned non-string (type %.200s)",                  Py_TYPE(res)->tp_name);    Py_DECREF(res);    return NULL; 

The manual states:

The return value must be a string object.

so you should do something like

def __str__(self):        return str(5)

or better, something more meaningful like

def __str__(self) -> str:        return "TestObject with id: {}".format(self.id)

(The return type can be added to the function decalarationso your editor will let you know if it doesn't have the right type.)