Python 101 : The "__str__()" function - String representation of an object -


The __str__() function is called to give us the string representation of an object, it is called when we call the print() function on an object for example.

Below of a class that doesn't implement the __str__() function:


We get the address of our function __str__() instead of a "string" representation of the class instance "c".

Below, we implement the __str__() function and we set it to display the "brand" and the "color" of the class "Car": 

Comments

Leave as a comment:

Archive