Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".
Questions tagged [repr]
281 questions
0
votes
1 answer
How do I remove quotes from a class initializer when reading from a file. Python
I'm storing the call to a class Task in an array in a .dat file. I'd like to read this file and reconstruct the class calls.
Here's the class that I'm using right now:
class Task:
def __init__(self, name, timespent):
self.name = name
…

Ivan Kelber
- 336
- 4
- 13
0
votes
1 answer
Why object of python class returns address instead of return data?
This is my first program in Python and I'm working with some API which is used to log in or register user.
This is how my class looks like:
class MyClass:
...
def __init__(self, apikey, gameid, securitykey, responseformat,
…

Vare Zon
- 309
- 1
- 6
- 13
0
votes
2 answers
__repr__ for an attribute that can be a string or None?
Given this simple class:
class Foo(object):
def __init__(self,a=None):
self.a = a
def __repr__(self):
return "Foo(a='{self.a}')".format(self=self)
I'm wondering if there is a simple way to get the __repr__ to return…

tom stratton
- 668
- 7
- 13
0
votes
2 answers
Is this abstract base class with a "better" __repr__() dangerous?
It bugs me that the default __repr__() for a class is so uninformative:
>>> class Opaque(object): pass
...
>>> Opaque()
<__main__.Opaque object at 0x7f3ac50eba90>
... so I've been thinking about how to improve it. After a little consideration, I…

Zero Piraeus
- 56,143
- 27
- 150
- 160
0
votes
1 answer
Handling long representation strings
How would you handle long __repr__ strings? Is there a best practice? Lets say I've got the following.
class Foo(object):
def __init__(self, bar):
self.bar = bar
def __repr__(self):
return…

P3trus
- 6,747
- 8
- 40
- 54
0
votes
2 answers
right usage of __repr__() in python?
I have a container class which I would like to use to print all the elements in it.
I'd like to print them to file or console.
I've laid out the element (Patch) and container class as below and __repr__(self).
I'm not sure I understood the purpose…

eugene
- 39,839
- 68
- 255
- 489
-1
votes
1 answer
Get __repr__ to return string that has same value when passed to eval?
I don't fully understand what is going on here. Why does the returned string from repr evaluate to False? If anyone can expand on what I'm not understanding here, that would be really appreciated.
class Bag(object):
def __init__(self, iter_vals…

newbiecoder11
- 107
- 5
-1
votes
1 answer
python __repr__(self): does not return
I have a below code in which the magic method repr(self) does not return my instance parameters below is the code
i am trying to learn the oops concept
class Item:
pay_rate = 0.8 # The pay rate after 20% discount
all = []
def…

Anshul Thakur
- 69
- 1
- 6
-1
votes
1 answer
Two values are same and different after defining a=b=class_name(value1) b=class_name(value2) in python
In the following code, I understand that the print of tree(named in the code) and parent(named in the code) should not be the same. But I do not understand, why the tree is always updating according to the updating of parent?
(This question…

Melina
- 293
- 3
- 11
-1
votes
3 answers
How to print special characters from string literal as their "backslash" symbol?
I have regular string literal and want to print it as it is - raw, similar how repr() function in Python does.
For example:
char* text = "this thing\n";
printf("%s", text);
How do I get C to print
this thing\n
instead of
this thing

cdpp
- 152
- 2
- 9
-1
votes
2 answers
Python __repr__ method not working as expected
The __repr__ function does not work in the following code:
class Minibar:
def __init__(self, drinks, snacks):
self.drinks=drinks
self.snacks= snacks
self.bill=0
def __repr__(self):
return "The minibar…

TxNull
- 1
-1
votes
4 answers
How can I fix the error with __repr__ in python?
I wanna create a function to generate a polynomial with corresponding coefficients.
class Polynomial:
def __init__(self, *coefficients):
self.coefficients = coefficients
def __len__(self):
return len(self.coefficients)
…

Lonnie Kim
- 11
- 4
-1
votes
3 answers
Is it possible to display special characters such as space, tab etc. when printing a string?
For example I have a string
line = r'I like cakes'
Is it possible to print it like this?
I\s like\s cakes
repr() is not working in this case

Anna Ignashkina
- 467
- 4
- 16
-1
votes
1 answer
Using __str__ to return a variable number of different lined strings?
I'm working on a project that creates "Facebook" in Python 3.x. The part I am currently stuck on is using the str function to return strings on different lines.
The code I am using for this is:
class Status:
likers = []
commentObjs = []
…

CoopStad
- 9
- 1
- 5
-1
votes
3 answers
comparing object with repr() is throwing NameError
So I'm trying to compare a Deck object with the evaluated representation of a Deck object and getting
Traceback (most recent call last):
File "C:/Users/Philipp/PycharmProjects/fnaround/src.py", line 3, in
print(Deck() ==…

phil
- 403
- 2
- 7