6

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
YoomarXD
  • 105
  • 1
  • 10
  • You need to actually call the method using `()`, e.g. `object.__repr__()`. Currently you are just referring to the method itself. A more appropriate call would be using the `repr()` builtin, e.g. `repr(object())`. – AChampion Nov 11 '16 at 22:00
  • @AChampion that doesn't work. Everyone keeps suggesting this without testing it. – Alex Hall Nov 11 '16 at 22:02
  • @AlexHall, not sure why you think this isn't correct. `class X(object): def __repr__(self): return 'Hello'; X().__repr__()` outputs the expected result as does `repr(X())`. – AChampion Nov 11 '16 at 22:05
  • @AChampion I had assumed OP meant the actual meaning of `object`, not that he had shadowed the name with an instance. – Alex Hall Nov 11 '16 at 22:08
  • Ahh, I assumed it was some generic reference because the base `object` type would print something if you just typed `object` in the interpreter. And `object.__repr__` would not be a bound method. – AChampion Nov 11 '16 at 22:26

4 Answers4

6

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))
boardrider
  • 5,882
  • 7
  • 49
  • 86
Anthon
  • 69,918
  • 32
  • 186
  • 246
1

Just use repr(object).

print(repr(object))
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

Try:

print(some_object.__repr__())

__repr__ needs to be called before you print it since it is a method unlike attributes such as __file__, __name__, etc., which do not need to be called (and cannot, for that matter). The same is true for the __str__() method: you need to call it with - some_object.__str__(), not some_object.__str__.

I assumed that the OP was referring to a general object with the word object, rather than the actual Python object called object, so I have used the variable name some_object instead. As was pointed out in the comments, if you literally do object.__repr__() this will raise an exception since __repr__() must be called on the instance (i.e., object().__repr__() will work).

elethan
  • 16,408
  • 8
  • 64
  • 87
0

You can use the old backticks

print(`object`)

in Python 2, or

print(repr(object))

in both Python 2 & 3

Useless
  • 64,155
  • 6
  • 88
  • 132