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

4

7

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?

Test answered 11/11, 2016 at 21:58 Comment(5)
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()).Mitinger
@Mitinger that doesn't work. Everyone keeps suggesting this without testing it.Leap
@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()).Mitinger
@Mitinger I had assumed OP meant the actual meaning of object, not that he had shadowed the name with an instance.Leap
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.Mitinger
R
7

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))
Rebuke answered 11/11, 2016 at 22:18 Comment(0)
A
1

Just use repr(object).

print(repr(object))
Agueda answered 11/11, 2016 at 22:1 Comment(4)
@AlexHall I guess I was thinking of __str__. I fixed the answer.Agueda
object.__str__() doesn't work either if that's what you're thinking.Leap
@AlexHall are you sure? I just created a class with a __str__ method, and both str(obj) and obj.__str__() worked. And in fact so does __repr__.Agueda
My mistake, I had assumed OP meant the actual meaning of object, not that he had shadowed the name with an instance.Leap
A
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).

Aspectual answered 11/11, 2016 at 22:1 Comment(3)
@AlexHall, I have tried it. Can you please explain how it is an error?Aspectual
@AlexHall oh are you referring literally to the python object object? i assumed the OP was using object simply to refer to any object, i.e., a string, a list, etc.Aspectual
Yes, I thought OP meant the actual type.Leap
G
0

You can use the old backticks

print(`object`)

in Python 2, or

print(repr(object))

in both Python 2 & 3

Gemination answered 11/11, 2016 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.