0
class TestClass:
    def __init__(self):
        pass
    def __str__(self):
        return 'You have called __str__'
    def __repr__(self):
        return 'You have called __repr__'

a = TestClass()
print(object.__repr__(a))
print(object.__str__(a))

Output:

<__main__.TestClass object at 0x7fe175c547c0>
You have called __repr__

What does those two functions do?


My understanding is that calling str(a) returns a.__str__() and calling repr(a) returns a.__repr__(). print(a) also prints the string, a.__str__() since there is an implicit str(a) conversion going on.

Note that my question is not a duplicate to another popular question; see my first comment below.

The behaviour is COUNTERINTUITIVE; doing print(object.__str__(a)) prints the repr string instead of the str string.

Adola
  • 337
  • 1
  • 16
  • 1
    Does this answer your question? [What is the difference between \_\_str\_\_ and \_\_repr\_\_?](https://stackoverflow.com/questions/1436703/what-is-the-difference-between-str-and-repr) – 0x263A Apr 25 '22 at 12:19
  • @0x263A It's different. It's not asking about `TestClass.__str__(a)`, but rather `object.__str__(a)`. Similarly, with the `__repr__` thing. In fact, the behavior is counterintuitive; `object.__str__(a)` prints the repr string and not the str string. – Adola Apr 25 '22 at 12:20
  • There's probably an answer here, but `object.__str__(a)` is not something you should ever sensibly do to begin with… – deceze Apr 25 '22 at 12:28
  • @deceze I am reading a book (Think Python); it uses that in one of the .py files linked in the book. – Adola Apr 25 '22 at 12:30
  • That's not… typical… – deceze Apr 25 '22 at 12:33
  • 1
    Shouldn't the output of `object.__str__(a)` be "You have called `__repr__`" in this example? – 0x263A Apr 25 '22 at 12:45
  • @0x263A Yes, I did say that. – Adola Apr 25 '22 at 13:27
  • @Adola in other words, your provided output does not match the actual output – 0x263A Apr 25 '22 at 13:39
  • @0x263A It matched. I tried copy-pasting and running the code in `www.programiz.com/python-programming/online-compiler/`, and the output is essentially the same. Maybe, you've misread. – Adola Apr 25 '22 at 13:42
  • 1
    `return 'You have called __repr__'` cannot produce the output "This is a repr string", and there's nowhere else this output could come from. – deceze Apr 25 '22 at 13:45
  • Sorry, it was a tiny mistake. – Adola Apr 25 '22 at 13:47
  • It seems your question has been answered but that site you provided outputs, "You have called __repr__" (as expected) you provided the output: "This is a repr string". Those are very different outputs. The reason this matters is because you stated you were using a book to learn and I was curious if the authors of the book had altered some function/if your question was perhaps missing a piece of information. – 0x263A Apr 25 '22 at 13:47
  • @deceze If you're interested about the usage in the book see https://github.com/AllenDowney/ThinkPython2/blob/master/code/BadKangaroo.py and https://github.com/AllenDowney/ThinkPython2/blob/master/code/GoodKangaroo.py – Adola Apr 25 '22 at 13:53
  • *"WARNING: this program contains a NASTY bug. I put it there on purpose as a debugging exercise, but you DO NOT want to emulate this example!"*…? – deceze Apr 25 '22 at 14:02
  • @deceze There are two links, the first is the buggy one and the second is the correct one. The bug is using a mutable type (list) as an optional argument which is potentially dangerous. It's irrelevant to the question here. :) – Adola Apr 25 '22 at 14:04
  • `object.__str__` is probably used there explicitly in order to avoid an infinite recursion in the string printing, which would also give away the actual bug and/or simply crash the program… – deceze Apr 25 '22 at 14:04

1 Answers1

4

The object class provides default implementations for the __repr__ and __str__ methods.

  • object.__repr__ displays the type of the object and its id (the address of the object in CPython)

  • object.__str__(a) calls repr(a). The rationale is that if __str__ is not overriden in a class str(a) will automatically call repr(a), using a possible override of __repr__. More exactly, the official documentation of the Python Language Reference / Data Model / Special method names / Basic customization says:

    object.__str__(self)

    ...The default implementation defined by the built-in type object calls object.__repr__().

    ... which in turn calls the overriden __repr__ method like repr(object) would have done.

This is exactly what happens here because you are directly calling object.__str__ while this is not expected.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I actually suspected this, since when the `__str__` is not defined, it will return `__repr__` instead when calling `str(a)`. Is this on the Python's official documentation? Where did you learn this? – Adola Apr 25 '22 at 13:31
  • 1
    @Adola: I have edited my answer with a reference to the official doc. – Serge Ballesta Apr 25 '22 at 16:26