8. Converting an Object to a String

When we’re working with classes and objects, it is often necessary to print an object (that is to print the state of an object). Consider the example below.

The print function shown above produces a string representation of the Point p. The default functionality provided by Python tells you that p is an object of type Point. However, it does not tell you anything about the specific state of the point.

We can improve on this representation if we include a special method call __repr__. Notice that this method uses the same naming convention as the constructor: two underscores before and after the name. It is common that Python uses this naming technique for special methods. In fact, there’s even a special name for it, dunder, a shortening of “double underscore”.

The __repr__ method is responsible for returning a string representation as defined by the class creator. In other words, you as the programmer get to choose what a Point should look like when it gets printed. In this case, we have decided that the string representation will include the values of x and y as well as some identifying text. It is required that the __repr__ method create and return a string.

When we run the program above you can see that the print function now shows the string that we chose. This is because the print function invokes the __repr__ method.

It is also possible to define a __str__ method for the object which does essentially the same thing as the __repr__ method. If we were to do this, then the print function would invoke the __str__ method. Technically, this is the first thing it tries to do, and then it falls back on invoking the __repr__ method.

You may be wondering, why create a __str__ method in our class when Python already provides one which we’ve used via str(). But, as we saw earlier, these automatic mechanisms do not do exactly what we want. Python provides many default implementations for methods that we as programmers will probably want to change. When a programmer changes the meaning of a special method we say that we override the method. Now the str() type converter function will use whatever __str__ method we provide.