Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".
Questions tagged [repr]
247 questions
3037
votes
24 answers
What is the difference between __str__ and __repr__?
What is the difference between __str__ and __repr__ in Python?
![](../../users/profiles/165495.webp)
Casebash
- 100,511
- 79
- 236
- 337
184
votes
9 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,…
![](../../users/profiles/452521.webp)
thr
- 18,022
- 19
- 89
- 129
155
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…
![](../../users/profiles/799882.webp)
0101amt
- 2,358
- 3
- 18
- 20
74
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?
![](../../users/profiles/1014938.webp)
Zero Piraeus
- 47,176
- 24
- 135
- 148
69
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.
![](../../users/profiles/263191.webp)
photon
- 1,239
- 1
- 11
- 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…
![](../../users/profiles/42973.webp)
Eric O Lebigot
- 81,422
- 40
- 198
- 249
34
votes
1 answer
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…
![](../../users/profiles/5399734.webp)
nalzok
- 11,870
- 17
- 57
- 105
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
…
![](../../users/profiles/2818583.webp)
AnV
- 2,486
- 3
- 28
- 36
24
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.
![](../../users/profiles/165039.webp)
Ian Dalton
- 359
- 2
- 8
20
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
>>>…
![](../../users/profiles/386279.webp)
beardc
- 16,995
- 16
- 66
- 88
19
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…
![](../../users/profiles/11115679.webp)
Granny Aching
- 1,055
- 5
- 30
18
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…
![](../../users/profiles/7938508.webp)
dec0de_d00dle
- 346
- 1
- 3
- 13
17
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,…
![](../../users/profiles/6676101.webp)
Toothpick Anemone
- 2,131
- 10
- 25
17
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.
![](../../users/profiles/3479125.webp)
Ivan Borshchov
- 2,215
- 3
- 26
- 56
11
votes
4 answers
Recursive reference to a list within itself
So I came across something very weird in python. I tried adding a reference to the list to itself. The code might help demonstrate what I am saying better than I can express. I am using IDLE editor(interactive…
![](../../users/profiles/2855159.webp)
Guy
- 585
- 2
- 21