Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

461
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda