Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".
Questions tagged [repr]
281 questions
11
votes
1 answer
What are the best practices for __repr__ with collection class Python?
I have a custom Python class which essentially encapsulate a list of some kind of object, and I'm wondering how I should implement its __repr__ function. I'm tempted to go with the following:
class MyCollection:
def __init__(self, objects = []):
…

abey
- 593
- 10
- 26
11
votes
4 answers
Python __repr__ for all member variables
Implementing __repr__ for a class Foo with member variables x and y, is there a way to automatically populate the string? Example that does not work:
class Foo(object):
def __init__(self, x, y):
self.x = x
self.y = y
def…

BoltzmannBrain
- 5,082
- 11
- 46
- 79
11
votes
1 answer
Tell IPython to use an object's `__str__` instead of `__repr__` for output
By default, when IPython displays an object, it seems to use __repr__.
__repr__ is supposed to produce a unique string which could be used to reconstruct an object, given the right environment.
This is distinct from __str__, which supposed to…

DanielSank
- 3,303
- 3
- 24
- 42
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…

Guy
- 604
- 5
- 21
10
votes
1 answer
Which is a better __repr__ for a custom Python class?
It seems there are different ways the __repr__ function can return.
I have a class InfoObj that stores a number of things, some of which I don't particularly want users of the class to set by themselves. I recognize nothing is protected in python…

Brian
- 913
- 1
- 9
- 13
10
votes
3 answers
Python: Maximum recursion depth exceeded when printing custom exception
The following code throws RuntimeError: maximum recursion depth exceeded while getting the str of an object. I can resolve the infinite recursion in two different ways, but I don't understand why each fix works and thus don't know which to use, or…

andylytical
- 113
- 7
10
votes
4 answers
In Python, what does '' mean?
What does mean? Example:
>>> def main():
... pass
...
>>> main
And maybe there is a way to somehow access it using 0x7f95cf42f320?

JadedTuna
- 1,783
- 2
- 18
- 32
9
votes
1 answer
Apply control characters to a string - Python
I'm trying to apply control characters, such as '\x08 \x08' that should remove the precedent char, to a string (move backwards, write space, move backwards)
For example when I type into python console :
s = "test\x08 \x08"
print s
print repr(s)
I…

Perceval W
- 448
- 6
- 11
8
votes
3 answers
What is the difference between super().__repr__() and repr(super())?
In python (3.5.2), I was expecting the repr(obj) function to call the magic method __repr__() of obj's class.
However calling both of them do not seem to yield the same result. Can anyone explain why ?
Sample code :
class parent:
def…

Camion
- 1,264
- 9
- 22
8
votes
2 answers
How to use __repr__ to create new object from it?
This is something I don't really get. I am trying to use __repr__ to create a new object from its output.
I have a class, OrderedSet, which contains a list and methods to organize it.
The str method of this class is
def __str__(self):
s = "Set…

Saphire
- 1,812
- 1
- 18
- 34
8
votes
3 answers
How to Give a C++ Class a Python __repr__() with SWIG
I've observed that when one types
help
in the Python repl, one gets
Type help() for interactive help, ...
and when one types
help()
one gets kicked into help mode. I'm pretty sure this is because site._Helper defines __repr__() (for the first…

Don Wakefield
- 8,693
- 3
- 36
- 54
7
votes
4 answers
How to make __repr__ to return unicode string
I call a __repr__() function on object x as follows:
val = x.__repr__()
and then I want to store val string to SQLite database. The problem is
that val should be unicode.
I tried this with no success:
val = x.__repr__().encode("utf-8")
and
val =…

xralf
- 3,312
- 45
- 129
- 200
7
votes
1 answer
Exclude default fields from python `dataclass` `__repr__`
Summary
I have a dataclass with 10+ fields. print()ing them buries interesting context in a wall of defaults - let's make them friendlier by not needlessly repeating those.
Dataclasses in Python
Python's @dataclasses.dataclass() (PEP 557) provides…

tony
- 870
- 7
- 16
7
votes
3 answers
How to create a __repr__ of a namedtuple?
How do I create a special method __repr__ where I can print, for example, '6 of spades' or 'Q of diamonds'?
How do I access the data from the namedtuple, keeping in mind that I have a list of namedtuples in self._cards?
import collections
cards =…

Piontk
- 93
- 1
- 6
7
votes
2 answers
Django: Assigning ForeignKey - Unable to get repr for class
I ask this question here because, in my searches, this error has been generally related to queries rather than ForeignKey assignment.
The error I am getting occurs in a method of a model. Here is the code:
class Deal(models.Model):
…

PANDA Stack
- 1,293
- 2
- 11
- 30