Inheritance comparison and string representation
1. Inheritance comparison and string representation
Great job with comparisons! Let's dive further into this topic, then move on to how we can display our custom objects' data.2. Comparing objects from different classes
We saw how to modify the eq method in a custom class so it compares the attributes and types of two objects.3. Comparing objects with inheritance
But what will happen if the second object we compare to has inherited from the first one?4. Bank and savings accounts
Remember our bank and savings account classes from earlier in the course?5. __eq__ in parent/child classes
Here, we've added eq methods to the BankAccount and the SavingsAccount class, a child class inherited from BankAccount. They include print statements, so when we compare objects, we'll see which method is called.6. Comparing parent and child objects
We can now create one object for each class and compare them. Notice that the child class' eq method was called and evaluated as False as they are different. Regardless of the order of comparison, the results are the same because Python will always call the child's eq method.7. Printing an object
We know how to compare objects, but how do we display them? Remember, in the last video, we saw 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 list we'll see the actual data contained in the object. Displaying objects is an essential component of software, so let's explore how we can do this with our custom classes.8. 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-underscore 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 print it in the console without explicitly calling print. The difference is that str gives an informal representation that is 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, our NumPy array shows the exact method call that was used to create the object. If we only implement one of them, we should choose repr because it is also used as a fall-back for print when str is not defined.9. Implementation: repr
Starting with the repr method, we can define it to accept one argument, self, and return a string. The return statement uses an f-string, which is where we write "f" right before our quotes. This technique allows the insertion of variables inside curly brackets. Here, we provide self.name and self.balance. We include single quotes around self.name to ensure that repr returns the code that can be used to reproduce the object - the name as a string and balance as a number. We can create an object and then display it, which shows the output of repr. As this class doesn't have the str method, repr will be used if we call the print method on the object as well.10. Implementation: str
The str method also 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. We use triple quotes to define multi-line strings in this case and again use an f-string to provide variables. Now, if we create a customer object and print it, we will see a user-friendly output.11. 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.