Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

I am trying to understand the __str__ method in Python.

class Test:
    def __str__(self):
        return 5

t = Test()

print(t.__str__())

In this method it returns an integer value but the print method is able to print it.

But, when I tried print(t) it threw the error TypeError: __str__ returned non-string (type int).

As I understand print(t) is also calling the __str__(self) method.

Why didn't print(t.__str__()) want the the string type conversion?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

len() --> __len__() + some checks
str() --> __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__())  # fine
print(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).

over 4 years ago · Santiago Trujillo Report

0

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 decalaration so your editor will let you know if it doesn't have the right type.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!