Advertisement
Python253

multiple_inheritance_demo

May 3rd, 2024
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: multiple_inheritance_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script demonstrates multiple inheritance in Python.
  9. It defines three classes:
  10.  
  11.    - Parent1: Represents a parent class with an attribute 'x' and a method 'method_parent1'.
  12.    - Parent2: Represents another parent class with an attribute 'y' and a method 'method_parent2'.
  13.    - Child:   Inherits from both 'Parent1' and 'Parent2', showcasing multiple inheritance.
  14.    - Note:    It has its own method 'method_child'.
  15.  
  16. Requirements:
  17. - Python 3.x
  18.  
  19. Functions:
  20. - line(): A utility function to print divider lines for better readability.
  21.  
  22. Usage:
  23. 1. Run the script using Python 3.x.
  24. 2. The script will instantiate the Child class with arguments representing inheritance.
  25. 3. It will then print output with explanations for each part of the demonstration, including method calls and isinstance checks.
  26.  
  27. Additional Notes:
  28. - This script serves as a basic example to understand the concept of multiple inheritance in Python.
  29. - It's recommended to run the script in an environment with Python 3.x installed to ensure compatibility.
  30. """
  31.  
  32. class Parent1:
  33.     def __init__(self, x):
  34.         self.x = x
  35.    
  36.     def method_parent1(self):
  37.         print(f"Parent1 method, x = {self.x}")
  38.  
  39. class Parent2:
  40.     def __init__(self, y):
  41.         self.y = y
  42.    
  43.     def method_parent2(self):
  44.         print(f"Parent2 method, y = {self.y}")
  45.  
  46. class Child(Parent1, Parent2):
  47.     def __init__(self, x, y):
  48.         super().__init__(x)
  49.         self.y = y
  50.  
  51.     def method_child(self):
  52.         print("Child method")
  53.  
  54. # Function to print divider lines
  55. def line():
  56.     print("-" * 91)
  57.    
  58. # Instantiate Child with arguments representing inheritance
  59. obj = Child(253, "Multiple Inheritance Demo")
  60.  
  61. # Output explanation
  62. line()
  63. print("\n\t\t\t\t:: Output with explanation ::\n")
  64. line()
  65.  
  66. # Explanation for calling method_parent2 from Parent2 class
  67. print("Calling method_parent2 from Parent2 class...\n")
  68. print("\t\t\t[Parent2 method, y = 'Multiple Inheritance']\n")
  69. print("- The method_parent2 method from the Parent2 class was called successfully.")
  70. print("- It prints the value of the attribute 'y' which is passed during initialization of Child.\n")
  71. line()
  72.  
  73. # Explanation for calling method_child from Child class
  74. print("Calling method_child from Child class...\n")
  75. print("\t\t\t[Child method]\n")
  76. print("- This confirms that the method_child method from the Child class was called successfully.")
  77. print("- Proof that the Child class can define its own methods, independent of its parent classes.\n")
  78. line()
  79.  
  80. # Explanation for isinstance checks
  81. print("\n\t\t\t\t:: Isinstance checks ::\n")
  82. line()
  83. print("Calling isinstance checks...\n")
  84. print("\tisinstance(obj, Parent1):", isinstance(obj, Parent1))
  85. print("\tisinstance(obj, Parent2):", isinstance(obj, Parent2))
  86. print("\tisinstance(obj, Child):  ", isinstance(obj, Child))
  87. print("\n- These checks confirm the inheritance relationships between classes.\n")
  88. line()
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement