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?
()
, e.g.object.__repr__()
. Currently you are just referring to the method itself. A more appropriate call would be using therepr()
builtin, e.g.repr(object())
. – Mitingerclass X(object): def __repr__(self): return 'Hello'; X().__repr__()
outputs the expected result as doesrepr(X())
. – Mitingerobject
, not that he had shadowed the name with an instance. – Leapobject
type would print something if you just typedobject
in the interpreter. Andobject.__repr__
would not be a bound method. – Mitinger