Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".
Questions tagged [repr]
281 questions
3635
votes
28 answers
What is the difference between __str__ and __repr__?
What is the difference between __str__ and __repr__ in Python?

Casebash
- 114,675
- 90
- 247
- 350
222
votes
12 answers
Accessing Object Memory Address
When you call the object.__repr__() method in Python you get something like this back:
<__main__.Test object at 0x2aba1c0cf890>
Is there any way to get a hold of the memory address if you overload __repr__(), other then calling super(Class,…

thr
- 19,160
- 23
- 93
- 130
160
votes
5 answers
Understanding repr( ) function in Python
repr(): evaluatable string representation of an object (can "eval()"
it, meaning it is a string representation that evaluates to a Python
object)
In other words:
>>> x = 'foo'
>>> repr(x)
"'foo'"
Questions:
Why do I get the double quotes when I…

0101amt
- 2,438
- 3
- 23
- 21
91
votes
2 answers
Why do backslashes appear twice?
When I create a string containing backslashes, they get duplicated:
>>> my_string = "why\does\it\happen?"
>>> my_string
'why\\does\\it\\happen?'
Why?

Zero Piraeus
- 56,143
- 27
- 150
- 160
76
votes
3 answers
What does !r do in str() and repr()?
According to the Python 2.7.12 documentation:
!s (apply str()) and !r (apply repr()) can be used to convert
the value before it is formatted.
>>> import math
>>> print 'The value of PI is approximately {}.'.format(math.pi)
The value of PI is…

nalzok
- 14,965
- 21
- 72
- 139
75
votes
9 answers
What is the meaning of %r?
What's the meaning of %r in the following statement?
print '%r' % (1)
I think I've heard of %s, %d, and %f but never heard of this.

photon
- 1,309
- 2
- 12
- 13
66
votes
3 answers
Best output type and encoding practices for __repr__() functions?
Lately, I've had lots of trouble with __repr__(), format(), and encodings. Should the output of __repr__() be encoded or be a unicode string? Is there a best encoding for the result of __repr__() in Python? What I want to output does have…

Eric O. Lebigot
- 91,433
- 48
- 218
- 260
33
votes
1 answer
str() vs repr() functions in python 2.7.5
what is the difference between str() and repr() functions in python 2.7.5?
Explanation on python.org:
The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate
…

AnV
- 2,794
- 3
- 32
- 43
27
votes
9 answers
Java equivalent of Python repr()?
Is there a Java method that works like Python's repr? For example, assuming the function were named repr,
"foo\n\tbar".repr()
would return
"foo\n\tbar"
not
foo
bar
as toString does.

Ian Dalton
- 389
- 3
- 8
24
votes
2 answers
Correct way to write __repr__ function with inheritance
I'm experimenting with OOP python and I wasn't sure about the __repr__ function inheritance. Since the parent class function looked like this:
def __repr__(self):
'''Returns representation of the object'''
return("{}({!r})".format("Class…

dec0de_d00dle
- 425
- 1
- 4
- 13
23
votes
4 answers
Possible to change a function's repr in python?
I've only seen examples for setting the __repr__ method in class definitions. Is it possible to change the __repr__ for functions either in their definitions or after defining them?
I've attempted without success...
>>> def f():
pass
>>>…

beardc
- 20,283
- 17
- 76
- 94
22
votes
1 answer
Reverse repr function in Python
if I have a string with characters ( 0x61 0x62 0xD ), the repr function of this string will return 'ab\r'.
Is there way to do reverse operation: if I have string 'ab\r' (with characters 0x61 0x62 0x5C 0x72), I need obtain string 0x61 0x62 0xD.

Ivan Borshchov
- 3,036
- 5
- 40
- 62
21
votes
3 answers
How can we get the default behavior of __repr__()?
If someone writes a class in python, and fails to specify their own __repr__() method, then a default one is provided for them. However, suppose we want to write a function which has the same, or similar, behavior to the default __repr__(). However,…

Toothpick Anemone
- 4,290
- 2
- 20
- 42
20
votes
4 answers
python displays `\n` instead of breaking a line
I wrote a function to create the VALUES part of a SQL query:
def query_values(data_iterator):
return ',\n'.join('\n({})\n'.format(',\n'.join('"{}"'.format(value) for value in data_row)
) for data_row in…

Granny Aching
- 1,295
- 12
- 37
12
votes
1 answer
SQLAlchemy best way to define __repr__ for large tables
I have a bunch of tables in SQLAlchemy that I want to define __repr__.
The standard convention seems to look like this:
def __repr__(self):
return "" % self.id
This is all well and good for small tables. However, I have…

Intrastellar Explorer
- 3,005
- 9
- 52
- 119