Reverse repr function in Python [duplicate]
I think what you’re looking for is ast.literal_eval: >>> s = repr(“ab\r”) >>> s “‘ab\\r'” >>> from ast import literal_eval >>> literal_eval(s) ‘ab\r’
I think what you’re looking for is ast.literal_eval: >>> s = repr(“ab\r”) >>> s “‘ab\\r'” >>> from ast import literal_eval >>> literal_eval(s) ‘ab\r’
Yes, if you’re willing to forgo the function actually being a function. First, define a class for our new type: import functools class reprwrapper(object): def __init__(self, repr, func): self._repr = repr self._func = func functools.update_wrapper(self, func) def __call__(self, *args, **kw): return self._func(*args, **kw) def __repr__(self): return self._repr(self._func) Add in a decorator function: def withrepr(reprfun): def … Read more
In order to format something in a string, a string representation of that something must first be created. “convert the value” is basically talking about how the string representation is to be constructed. In python, there are two fairly natural choices to get a string representation of something … str and repr. str is generally … Read more
You are looking at the default representation of a function object. It provides you with a name and a unique id, which in CPython happens to be a memory address. You cannot access it using the address; the memory address is only used to help you distinguish between function objects. In other words, if you … Read more
Background: In Python, there are two builtin functions for turning an object into a string: str vs. repr. str is supposed to be a friendly, human readable string. repr is supposed to include detailed information about an object’s contents (sometimes, they’ll return the same thing, such as for integers). By convention, if there’s a Python … Read more
The Python manual has this to say about id(): Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value. (Implementation note: this is the address of the … Read more
>>> x = ‘foo’ >>> x ‘foo’ So the name x is attached to ‘foo’ string. When you call for example repr(x) the interpreter puts ‘foo’ instead of x and then calls repr(‘foo’). >>> repr(x) “‘foo'” >>> x.__repr__() “‘foo'” repr actually calls a magic method __repr__ of x, which gives the string containing the representation … Read more
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 got single backslashes, just as you intended: >>> print(my_string) why\does\it\happen? The string below has three characters in it, not four: >>> ‘a\\b’ ‘a\\b’ >>> len(‘a\\b’) 3 You can get the standard … Read more
Alex summarized well but, surprisingly, was too succinct. First, let me reiterate the main points in Alex’s post: The default implementation is useless (it’s hard to think of one which wouldn’t be, but yeah) __repr__ goal is to be unambiguous __str__ goal is to be readable Container’s __str__ uses contained objects’ __repr__ Default implementation is … Read more