Skip to content Skip to sidebar Skip to footer

What Is The Relationship Between __repr__ And Eval(), And What Is The Main Purpose Of __repr__?

I just came across this question Difference between __str__ and __repr__ in Python and its mentioned that the main purpose of the __repr__ is to be unambiguous. But I heard that th

Solution 1:

For types that have a literal notation (str, int, float, list, etc.) the repr() returns a string that can be used to create the type again.

That is nice, but not a requirement.

The main purpose for __repr__ is to provide the developer with unambiguous information as to what object they have here, for debugging purposes.

Quoting from the __repr__ documentation:

Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned.

but most of all:

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

Emphasis mine.

Returning a constant string from your custom type would not break anything technically, but would make your life as a developer harder.

Solution 2:

The convention is that you use repr to get a string that represents the object, and str to describe it. repr is used more for debugging, str for regular printing. The idea is that the output of repr is something that looks like code you could eval, and often it is. However, you can't rely on that. If you have to convert an object to a string and back, use pickle or json.

For example:

>>>greeting = "Hello">>>printstr(greeting)
Hello

>>>printrepr(greeting)
'Hello'

If you are writing your own class, and it is very simple, you can make __repr__ return something that can be eval'd:

classSong(object):def__init__(self, title, artist):
        self.title = title
        self.artist = artist

    def__repr__(self):
        return"Song(%r, %r)" % (self.title, self.artist)

    def__str__(self):
        return"%s - %s" % (self.artist, self.title)

Note how I use %r to get the repr of the title and artist. This takes care of escaping and quoting (for python) automatically, and allows me to eval the result of repr(a_song), although I wouldn't do that outside of debugging. Again, str returns something you would print to the user, repr something that helps you debugging. If the class gets more complicated than this, you won't be able to return something complete or evalable from repr. The convention here is to return a concise string to identify you instance, usually with angular brackets:

>>>repr(type(None))
"<type 'NoneType'>"

>>>import gtk>>>win = gtk.Window()>>>repr(win)
'<gtk.Window object at 0x10de45690 (GtkWindow at 0x7fdd89240000)>'

Post a Comment for "What Is The Relationship Between __repr__ And Eval(), And What Is The Main Purpose Of __repr__?"