Questions tagged [repr]

Python built-in function that returns a string from an arbitrary object. Abbreviation for "representation".

docs: https://docs.python.org/3/library/functions.html#repr

281 questions
5
votes
3 answers

When is the output of repr useful?

I have been reading about repr in Python. I was wondering what the application of the output of repr is. e.g. class A: pass repr(A) ='' b=A() repr(b) = '<__main__.A instance at 0x74d78>' When would one be interested…
shaz
  • 71
  • 3
5
votes
3 answers

Understanding difference between Double Quote and Single Quote with __repr__()

What is the difference between print, object, and repr()? Why is it printing in different formats? See the output difference: >>> x="This is New era" >>> print x # print in double quote when with print() This is New era >>> x …
Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
5
votes
1 answer

Python - nested __repr__ reverts newline to "\\n"

class MyClass: def __init__(self): self.list_ = [] def __repr__(self): return '\n'.join(['this','should','all','be','on','separate','lines']) + str([str(list_val) for list_val in self.list_]) myClass =…
Jon McClung
  • 1,619
  • 20
  • 28
5
votes
3 answers

Force repr() to use single quotes

I have a question, is there a way to "force" repr() to create always single quotes around a string? This happens when I only use repr() print repr("test") 'test' print repr("test'") "test'" print repr("test\"") 'test"' print…
Marcono1234
  • 5,856
  • 1
  • 25
  • 43
5
votes
1 answer

How can I use `str.format` directly as `__repr__`?

Say I want to debug a simple class with an attribute myattribute. I create a repr method like this: class SimpleClass: def __repr__(self): return "{0.myattribute}".format(self) It feels a bit redundant, so I would prefer to use format…
emu
  • 1,597
  • 16
  • 20
5
votes
3 answers

Python style repr for char * buffer in c?

for the most part I work in Python, and as such I have developed a great appreciation for the repr() function which when passed a string of arbitrary bytes will print out it's human readable hex format. Recently I have been doing some work in C and…
john-charles
  • 1,417
  • 4
  • 17
  • 30
4
votes
1 answer

Bad string representation of negative imaginary numbers in Python

For some reason the string representation of negative imaginary numbers in Python is different for equal values: >>> str(-3j) '(-0-3j)' >>> str(0-3j) '-3j' Moreover, if I try to get the string representation of -0-3j, I get: >>>…
amitjans
  • 83
  • 1
  • 7
4
votes
2 answers

Can a dataclass field format its value for the repr?

I have a Node class holding RGB data in both hex and HSV form. I'll be using this to sort colors in various ways and would prefer the HSV tuple to remain in float form for comparisons instead of converting from a string for every use. Is there a way…
zecuse
  • 125
  • 1
  • 1
  • 8
4
votes
0 answers

Where/How is the function representation as shown in IPython REPL generated?

For example, in REPL: In [0]: def func(x=0, y=1): ...: return x In [1]: func Out [1]: But how do I print it or store in a variable? In [2]: print(func) Out [2]:
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
4
votes
1 answer

Common or pythonic __repr__ methods

I usually write a __repr__ as a way to show how the instance can be re-created in the first place. For example: class Component: def __init__(self, start, end): self.start = start self.end = end def __repr__(self): …
David542
  • 104,438
  • 178
  • 489
  • 842
4
votes
1 answer

EOF Error while using f-string in __repr__() function

I'm working in Python 3.x, and I'm trying to get an f-string to report from a __repr__ function, but I can't seem to get the following formatted string to work the way I'm expecting it to. I'm constantly getting "SyntaxError: unexpected EOF while…
GrantG
  • 43
  • 3
4
votes
2 answers

Why is \x00 not converted to \0 by repr

Here is an interesting oddity about Python's repr: The tab character \x09 is represented as \t. However this convention does not apply for the null terminator. Why is \x00 represented as \x00, rather than \0? Sample code: # Some facts to make sure…
Andrei Cioara
  • 3,404
  • 5
  • 34
  • 62
4
votes
1 answer

using R in jupyter: display_markdown in loop

I need to setup a reporting document in which I loop over items and generate individual graphs. when using R in jupyter i read about display_markdown and display_html using repr and IRDisplay here: How to render LaTeX / HTML in Jupyter (R)? This…
sektionschef
  • 123
  • 1
  • 8
4
votes
1 answer

lua equivalent of python repr

Is there an equivalent function to Python's repr() function in Lua? In other words a function that prints non-printable characters with \x where x is n or b etc, or \000 code if not a Lua string escape character. I've googled and can't find…
Oliver
  • 27,510
  • 9
  • 72
  • 103
4
votes
2 answers

How to Understand and Parse the Default Python Object Representations

When you print an object in Python, and __repr__ and __str__ are not defined by the user, Python converts the objects to string representations, delimited with angle brackets... > The…
Carl Smith
  • 3,025
  • 24
  • 36