When I am in the Python or IPython console, what is called when I am returned an output?
Asked Answered
A

2

10

For example,

python
>> x = 1
>> x
1

I'm curious about what method/function on x is returning 1. I'm asking because I'm seeing differences between calling print x and simply x.

Similary, is there a way to specify what is called? Does this configuration exist in IPython?

Affluence answered 3/2, 2015 at 16:38 Comment(7)
I think you're asking about the difference between __repr__ and __str__. Each one returns a string that describes an object, but they don't always return the same result.Clower
It probably comes down to that - but which is being used in the above case?Affluence
doing a local test, looks like reprAffluence
Yeah. Short answer: print uses str, and >> x uses repr.Clower
Wanna make that an answer?Affluence
Ok, I'll put something together real quick...Clower
See this answer for more information.Musette
C
10

When you inspect an object in that manner in a REPL, it invokes the object's __repr__ method. In comparison, print uses the object's __str__ method. Example:

>>> class Widget:
...     def __repr__(self):
...             return "repr of a Widget"
...     def __str__(self):
...             return "str of a Widget"
...
>>> x = Widget()
>>> x
repr of a Widget
>>> print(x)
str of a Widget
>>> print([x,2,3])
[repr of a Widget, 2, 3]
>>> print(repr(x))
repr of a Widget
>>> print(str(x))
str of a Widget

When defining __repr__ and __str__ for your own classes, try to follow the documentation's suggestions regarding which one should be more detailed and "official".

[__repr__ computes] the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).
...
[__str__ computes] the “informal” string representation of an object. The return value must be a string object. This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

Clower answered 3/2, 2015 at 16:45 Comment(2)
Worth noting that if __str__ isn't defined, print(x) returns x.__repr__()`Photoconductivity
The answer by @wim should be the accepted answer.Cleland
K
5

The other answer addresses repr in a vanilla Python REPL, but it neglected to answer about IPython, which works quite differently and has many more features (and complexity) in regards to REPL printing.

Here's an example discrepancy:

# vanilla python:
>>> type([])
<class 'list'>

# in IPython:
>>> type([])
list

IPython has a custom pretty printer and public hooks for customizing repr within IPython. One such hook is _repr_pretty_ (single underscores!) and here's a basic example:

>>> class Widget:
...     def __repr__(self):
...         "vanilla"
...     def _repr_pretty_(self, p, cycle):
...         p.text("chocolate, strawberry")
...         
>>> Widget()
chocolate, strawberry

For more features, see "Integrating your objects with IPython" in the docs, in particular the Rich display section.

Kiangsi answered 3/4, 2019 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.