Operator overloading: string representation
1. Operator overloading: string representation
Great job with comparison! Let's continue to improve the integration of our custom classes with Python's built-in operators.2. Printing an object
In the last video, we discovered that calling print on an object of a custom class returns the object's address in memory by default. But there are plenty of classes for which the printout is much more informative. For example, if we print a numpy array or a DataFrame, we'll see the actual data contained in the object.3. str vs repr
There are two special methods that we can define in a class that will return a printable representation of an object. The double-underscore-str-double-undersrore method is executed when we call print or str on an object, and the repr method is executed when we call repr on the object, or when we print it in the console without calling print explicitly. The difference is that str is supposed to give an informal representation, suitable for an end user, and repr is mainly used by developers. The best practice is to use repr to print a string that can be used to reproduce the object -- for example, with numpy array, this shows the exact method call that was used to create the object. If you only choose to implement one of them, chose repr, because it is also used as a fall-back for print when str is not defined.4. Implementation: str
Let's start by implementing the str method. It shouldn't accept any arguments besides self, and it should return a string. Here, the string representation of a customer will consist of the word Customer, then on the next line , name, colon, followed by the customer's name, then balance, colon, and the customer's balance. Just a quick reminder: the triple quotes are used in Python to define multi-line strings, and the format method is used on strings to substitute values inside curly brackets with variables. If we create a customer object now and call print on that object, we will see a user-friendly output.5. Implementation: repr
Similarly, we can implement the repr method, which also accepts one argument self and returns a string. Following best practices, we make sure that repr returns the string that can be used to reproduce the object, in this case, the exact initialization call. Then if we try to print the object in the console, we'll see the the output of repr. Moreover, in this class we didn't define the str method, so repr will be used as a fallback for the actual print method as well. Notice the single quotes around the name in the return statement. Without the quotes, the name of the customer would be substituted into the string as-is, but the point of repr is to give the exact call needed to reproduce the the object, where the name should be in quotes. Notice also that we can use single quotes inside double quotes and vice versa.6. Let's practice!
Now, head over to the exercises and try it out yourself!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.