I've read in this thread all the answers, but I got stuck in the following example:
class Person:
pass
p=Person()
According to this source, if you call repr(p)
you will get this as default:
<__main__.Person object at 0x7fb2604f03a0>
However, I called repr(p)
on the python shell and I got this:
>>> repr(p)
'<__main__.Person object at 0x000001F1C0B1A2E0>'
As far as my understanding goes, there's a huge difference between the both: the latter is python expression, in this way, it can be used in eval()
.
Because I got two different results, I tried to pin down the cause, which I think I did.
>>>print(repr(p))
<__main__.Person object at 0x0000021D032392E0>
According to the documentation, object.__str__(self)
is called when you use str(object)
and print(object)
, so I tried this:
x = print(repr(p))
y = str(repr(p))
>>> x == y
False
Comparison:
>>> print(repr(p))
<__main__.Person object at 0x0000021D032392E0>
>>> str(repr(p))
'<__main__.Person object at 0x0000021D032392E0>'
From this answer,
In the Python shell (interactive interpreter), if you enter a variable at the >>> prompt and press RETURN, the interpreter displays the results of repr() implicitly called on that object.
repr()
is being called behind the scenes, meaning that, on the python shell, >>> p
returns repr(p)
.
<__main__.Person object at 0x000002184030A2E0>
>>> repr(p)
returns repr(repr(p))
, that is, repr(<__main__.Person object at 0x000002184030A2E0>)
.
'<__main__.Person object at 0x000002184030A2E0>'
However, I'm not sure why print(repr(p))
and str(repr(p))
differ on the python shell, don't they both call the same function?