Understanding the MRO of a class
In this section, let’s explore how methods are resolved in a class that has no inheritance specified within its code. A class by default in Python 3 is inherited by object
. To understand how MRO works on a class that has no parent class, looking at it in its simplest form is the easiest approach. We will then see how MRO works on a class with single, multiple, and multilevel inheritance.
In this example, let’s create a class for a branch of ABC Megamart as follows:
- In the
Branch
class, let’s create attributes for branch ID, street, city, state and ZIP code, product, sales, and invoice. Let’s also create methods such asget_product
(which returns the product),get_sales
(which returns sales), andget_invoice
(which returns the invoice). The following code represents theBranch
class:class Branch: def __init__(self, branch_id, branch_street, ...