I know that in Python Shell when you type >>> object
it shows the object.__repr__
method and if you type >>> print(object)
it shows the object.__str__
method.
But my question is, is there a short way to print __repr__
while executing a Python file?
I mean, in a file.py if I use print(object)
it will show object.__str__
and if I just type object
it shows nothing.
I have tried using print(object.__repr__)
but it prints <bound method object.__repr__ of reprReturnValue>
Or is this impossible?
If you just want to print the representation and nothing else, then
print(repr(object))
will print the representation. Where your invocation went wrong were the missing parentheses, as the following works as well:
print(object.__repr__())
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))