21. Problem to create multiple classes in Python

In this Python oops program, we will create multiple classes in Python and access them using a single class. This example illustrates a way to handle objects of different classes using type-checking and conditional statements.

Create multiple classes in Python

Steps to solve the program
  1. Five classes (Class1, Class2, Class3, Class4, and Class5) are defined, each with a single method that prints a specific message.
  2. A list named classes is created, which contains instances of the five defined classes.
  3. A loop iterates over each object (obj) in the classes list.
    Inside the loop, conditional statements (if, elif) are used to check the type of the object using the isinstance() function. Depending on the object’s class, the corresponding method is called using dot notation.
  4. For example, if the object is an instance of Class1, the method1() of Class1 is called.
  5. This pattern repeats for each class, ensuring that the appropriate method is called for each object.

Output:

Related Articles

Python Class structure with module-level Import.

Create 5 Python classes and set up multilevel inheritance among all the classes.

Leave a Comment