I am learning the ropes in Python. When I try to print an object of class Foobar
using the print()
function, I get an output like this:
<__main__.Foobar instance at 0x7ff2a18c>
Is there a way I can set the printing behaviour (or the string representation) of a class and its objects? For instance, when I call print()
on a class object, I would like to print its data members in a certain format. How to achieve this in Python?
If you are familiar with C++ classes, the above can be achieved for the standard ostream
by adding a friend ostream& operator << (ostream&, const Foobar&)
method for the class.