Switching metaclasses
We can now look into the concept of switching metaclasses for a class. You may think, why do we need to switch metaclasses? Switching metaclasses reinforces the reusability concept of metaprogramming and in this case, it helps in understanding how a metaclass created for use on one class can also be used for a different class without impacting the class definition.
In the example for this section, we will be creating two meta classes – IncomeStatementMetaClass
and BalanceSheetMetaClass
. For the Malibu branch of ABC Megamart, we will create a class to capture the information required for its financial statements. The two financial statements relevant for this example are Income Statement attributes and Balance Sheet attributes for the Malibu branch. To differentiate where a particular attribute or method of a class should go, we will be creating two metaclasses that look at the names of the attributes and tag them under Income Statement or Balance Sheet...