Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".
Questions tagged [repr]
281 questions
4
votes
2 answers
dict's __repr__() in python2 and python3
I'm porting a python library from python 2 only to python 2 and 3 in one codebase (2.6, 2.7 and 3.3+). The main problem left is that a lot of tests use something like this:
def test(self):
example = {u'foo': u'bar'}
self.assertEqual(str(example),…

Christian Geier
- 2,059
- 2
- 21
- 27
4
votes
2 answers
PHP equivalent for Python's repr()
I'm creating a simple array wrapper class and want it's __toString() method to be formatted like a Python list, eg: ["foo", "bar", 6, 21.00002351]. Converting each element to a string is not enough, since string-objects are actually enquoted in the…

Niklas R
- 16,299
- 28
- 108
- 203
3
votes
0 answers
Python: `foo = object.method; foo is object.method` returns `False` despite identical reprs
>>> a_string = "this is a string"
>>> a_method = a_string.upper
>>> a_string.upper()
'THIS IS A STRING'
>>> a_method()
'THIS IS A STRING'
>>> a_string.upper
>>> a_method

dain
- 672
- 1
- 7
- 22
3
votes
3 answers
Formatted Python string uses neither repr nor str - what is happening?
I have an enumeration ResourceType that inherits from both namedtuple and Enum, and I don't override __str__ or __repr__ anywhere. When I format an instance of that enum I unexpectedly get just the undecorated value, as opposed to either the repr()…

pallgeuer
- 1,216
- 1
- 7
- 17
3
votes
0 answers
Using the `print()` and the `repr() ` functions in python alters the number of decimal digits
I try to specify the number of useful digits in case of a numpy array of floats by using the numpy.round() function. After, I specify e.g. to use 16 digits and evaluate the print() or the repr() function on a specific array item I end up with a…

Gergely Mathe
- 93
- 2
- 11
3
votes
2 answers
How come python's __repr__ of a dict is more readable than its __str__?
See the below example:
In [33]: test = {x:str(x)*10 for x in range(10)}
In [35]: print test
{0: '0000000000', 1: '1111111111', 2: '2222222222', 3: '3333333333', 4: '4444444444', 5: '5555555555', 6: '6666666666', 7: '7777777777', 8: '8888888888', 9:…
anon
3
votes
1 answer
Why does sympy.init_printing change set notation?
When I call sympy.init_printing(), set notation changes from {a, b, c} to set([a, b, c]). Why does this happen?
In [1]: import sympy
In [2]: (x, y, z) = sympy.symbols("x y z")
In [3]: x+y**z
Out[3]: x + y**z
In [4]: (x+y**z).free_symbols
Out[4]:…

gerrit
- 24,025
- 17
- 97
- 170
3
votes
2 answers
How can a string representation of a NumPy array be converted to a NumPy array?
The function numpy.array_repr can be used to create a string representation of a NumPy array. How can a string representation of a NumPy array be converted to a NumPy array?
Let's say the string representation is as follows:
array([-0.00470366, …

d3pd
- 7,935
- 24
- 76
- 127
3
votes
3 answers
Why doesn't this __repr__ function return a string?
class Person:
greeting = 'Hello'
def __repr__(self):
return self.greeting
>>> Sam = Person()
>>> Sam.greeting
'Hello'
>>> Sam
Hello
I am having difficulty understanding why the __repr__ function returns self.greeting without…

springle
- 336
- 2
- 9
3
votes
2 answers
Formatting floating-point numbers without loss of precision in AngularJS
In AngularJS how do I output a floating point number on an HTML page without loss of precision and without unnecessary padding with 0's?
I've considered the "number" ng-filter (https://docs.angularjs.org/api/ng/filter/number) but the fractionSize…

hjort
- 133
- 1
- 7
3
votes
4 answers
How to walk through a python array/object similar to PHP's foreach function
I'm new to Python, sorry for asking such a probably simple question.
I'm hacking a script which has an Array(?) which I can print with the following command:
repr(Interfaces.log_manager.job_log[user_id])) gives me:
{
'3f2': ('3', 0.0078125,…

biergardener
- 185
- 1
- 1
- 11
3
votes
1 answer
python: unexplainable infinite recursion with __repr__
Here's a piece of code, which goes into an infinite recursion loop, which consists only of __repr__ function, seemingly calling itself. But I really can't see, how it calls itself. Moreover, I can't even understand, how it was called:
class…

Boris Burkov
- 13,420
- 17
- 74
- 109
3
votes
1 answer
Class-based decorator and repr() conservation
I was trying to have my class-based decorator keeping the repr() behavior of the original wrapped function (to match the way the functools.wraps decorator works on functions). I am using python 3.3.
First I tried functools:
import functools
class…

Maxime Rossini
- 3,612
- 4
- 31
- 47
3
votes
2 answers
__repr__ vs repr
Is there a difference between the two methods?
For example,
from datetime import date
today = date(2012, 10, 13)
repr(today)
'datetime.date(2012, 10, 13);
today.__repr__()
'datetime.date(2012, 10, 13)'
They seem to do the same thing, but why would…

Billy Thompson
- 469
- 5
- 8
- 15
2
votes
2 answers
python unittest failing assertion with overloaded __repr__
In my code, I've defined a class with its own repr method. The representation of the class should be a list.
def __repr__(self):
if self.front <= self.tail:
q = self._queue[self.front:self.tail+1]
elif self.front > self.tail:
…

bobbypins
- 134
- 2
- 8