repr

Why can __repr__ function use repr() in itself when defining a class?(Python)

Why can __repr__ function use repr() in itself when defining a class?(Python) Question: class Link: def __repr__(self): if self.rest is not Link.empty: rest_repr = ‘, ‘ + repr(self.rest) else: rest_repr = ” return ‘Link(‘ + repr(self.first) + rest_repr + ‘)’ I wonder :Is the repr function a built-in funciton in Python even though I am …

Total answers: 1

RecursionError when inheriting from float and using str and repr

RecursionError when inheriting from float and using str and repr Question: I was testing some features in Python for fun 😉 But I have a recursion error that I don’t understand class Test(float): def __new__(cls, value): return super().__new__(cls, value) def __str__(self): return super().__str__() def __repr__(self): return f'<value: {str(self)}>’ test = Test(12) print(test) Traceback: Traceback (most …

Total answers: 2

What are the best practices for __repr__ with collection class Python?

What are the best practices for __repr__ with collection class Python? Question: 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 = []): self._objects = [] …

Total answers: 1

What does !r do in str() and repr()?

What does !r do in str() and repr()? Question: 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 approximately 3.14159265359. >>> print ‘The …

Total answers: 2

Reverse repr function in Python

Reverse repr function in Python Question: if I have a string with characters ( 0x61 0x62 0xD ), the repr function of this string will return ‘abr’. Is there way to do reverse operation: if I have string ‘abr’ (with characters 0x61 0x62 0x5C 0x72), I need obtain string 0x61 0x62 0xD. Asked By: Ivan …

Total answers: 1

Why do backslashes appear twice?

Why do backslashes appear twice? Question: When I create a string containing backslashes, they get duplicated: >>> my_string = "whydoesithappen?" >>> my_string ‘why\does\it\happen?’ Why? Asked By: Zero Piraeus || Source Answers: What you are seeing is the representation of my_string created by its __repr__() method. If you print it, you can see that you’ve actually …

Total answers: 2

str() vs repr() functions in python 2.7.5

str() vs repr() functions in python 2.7.5 Question: 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 representations which can be read by the interpreter (or will force a …

Total answers: 1

Understanding repr( ) function in Python

Understanding repr( ) function in Python Question: 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 do repr(x)? (I don’t get them …

Total answers: 5

Best output type and encoding practices for __repr__() functions?

Best output type and encoding practices for __repr__() functions? Question: 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 non-ASCII characters. I …

Total answers: 3

What is the meaning of %r?

What is the meaning of %r? Question: 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. Asked By: photon || Source Answers: It calls repr() on the object and inserts the resulting string. Answered By: Ignacio Vazquez-Abrams …

Total answers: 9