2

Why does the Python interpreter use repr() to print expressions?

For example:

>>> S = "First line.\nSecond line."
>>> S
'First line.\nSecond line.'

Of course I can trigger str() by the print statement:

>>> print S
First line.
Second line.

But why is str() not triggered by default in the interactive mode? It seems to be a better choice in many respects.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Drizzt
  • 143
  • 7
  • 3
    Complete speculation: In an interactive environment, you're likely to copy-paste the result of one expression into a new expression. If this is a common use case, then `repr` is more useful than `str`. – Kevin Feb 25 '16 at 15:45
  • 2
    Because it makes a lot more sense for interactive debugging of **complex** values!? If an expression results in a class instance, a simple `print` may give you no or misleading information. Try printing an instance of `datetime` for example... is that a string, or an object...? – deceze Feb 25 '16 at 15:49
  • 4
    *"seems to be a better choice in many respects"* - the example you've provided doesn't show this at all, the `repr` is unambiguous where the string has unprintable characters and whitespace. – jonrsharpe Feb 25 '16 at 15:49
  • 1
    Because.... this is what repr is for? otherwise, why to have both str and repr. – ddbug Feb 25 '16 at 15:49
  • 4
    How would you tell `'3'` and `3` apart? Is `2000-01-01 00:00:00` a string or a datetime? – Steven Rumbalski Feb 25 '16 at 15:53
  • 4
    The REPL is for programmers, not end-users. It is clear that `repr` is much more useful to the programmer than `str` – John Coleman Feb 25 '16 at 15:53
  • If a string contains control characters, printing it could break your terminal, among other things. – wjandrea Nov 21 '18 at 18:14
  • In the Python interpreter/shell, when one types a variable/expression, it will execute `print(repr(myExpressionOrVar))` on it. – flow2k Jun 24 '19 at 04:00

2 Answers2

6

str() is used for "pretty printing", but in the Python interpreter, you are more likely to want the background information such as the id of the object, what class it is, etc. (theoretically), and repr() is used for that. If you run pydoc __builtin__.repr, it says "For most object types, eval(repr(object)) == object." There are some types that are completely different, but whose __str__ method returns the same thing. For example, str(str(some_object)) is the same as str(same_object), so if I had a list, [0, 4, 5], and I printed it with str, I would get [0, 4, 5]. If I printed str(mylist) with str(), I would still get [0, 4, 5] even though they are different types. With repr(), however, str(mylist) would be printed as '[0, 4, 5]' which is more likely what I was looking for. Someone who is using the interpreter is probably testing something or debugging something, and he would want to know that it was a string, not really a list.

zondo
  • 19,901
  • 8
  • 44
  • 83
1

repr is a method that returns how an object is represented inside your program, while str returns how it should be show to the outside.

For example

class Person(object):
    def __init__(self, name, age, passport):
        self.name = name
        self.age = age
        self.passport = passport
    def __repr__(self):
        return 'Name: {0}\nAge: {1}\nPassport: {2}'.format(self.name, self.age, self.passport)
    def __str__(self):
        return 'Hi, my name is {0}. How are you?'.format(self.name)

Then in an interactive session you can do:

>>> paul = Person('Paul', 30, 'XXXXXXXXX1')
>>> paul
Name: paul
Age: 30
Passport: XXXXXXXXX1
>>> print(paul)
Hi, my name is Paul. How are you?
Mr. E
  • 2,070
  • 11
  • 23