Iterate and Initialize all sub classes in python

In various situations, we need a mechanism to Initialize all Sub classes for a given Super class. For instance, let’s assume that we have a Super class called ArithmeticOperation and another 4 classes derived from it (Addition, Subtraction, Multiplication and Division).

One way of doing it is Initializing each Sub class separately.

But if there are hundreds of Sub classes, we need to think of another mechanism.  That’s where .__subclasses__() becomes very useful. We can use that method to Iterate and Initialize all Sub classes for that given Super Class. You can do it like this,

Hope this post helped someone who’s looking for a way to Iterate and Initialize all Sub classes for a given Super Class using python. 🙂