YoomarXD YoomarXD - 1 year ago 81
Bash Question

How to print __repr__ in a Python3 file? (not in shell)

I know that in Python Shell when you type

>>> object
it shows the
method and if you type
>>> print(object)
it shows the

But my question is, is there a short way to print
while executing a Python file?

I mean, in a file.py if I use
it will show
and if I just type
it shows nothing.

I have tried using
but it prints
<bound method object.__repr__ of reprReturnValue>

Or is this impossible?

Answer Source

If you just want to print the representation and nothing else, then


will print the representation. Where your invocation went wrong were the missing parenthesis, as the following works as well:


If however you want this to be part of more information and you are using string formatting, you don't need to call repr(), you can use the conversion flag !r

print('The representation of the object ({0!r}) for printing,'
      ' can be obtained without using "repr()"'.format(object))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download