Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: multiple_inheritance_demo.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script demonstrates multiple inheritance in Python.
- It defines three classes:
- - Parent1: Represents a parent class with an attribute 'x' and a method 'method_parent1'.
- - Parent2: Represents another parent class with an attribute 'y' and a method 'method_parent2'.
- - Child: Inherits from both 'Parent1' and 'Parent2', showcasing multiple inheritance.
- - Note: It has its own method 'method_child'.
- Requirements:
- - Python 3.x
- Functions:
- - line(): A utility function to print divider lines for better readability.
- Usage:
- 1. Run the script using Python 3.x.
- 2. The script will instantiate the Child class with arguments representing inheritance.
- 3. It will then print output with explanations for each part of the demonstration, including method calls and isinstance checks.
- Additional Notes:
- - This script serves as a basic example to understand the concept of multiple inheritance in Python.
- - It's recommended to run the script in an environment with Python 3.x installed to ensure compatibility.
- """
- class Parent1:
- def __init__(self, x):
- self.x = x
- def method_parent1(self):
- print(f"Parent1 method, x = {self.x}")
- class Parent2:
- def __init__(self, y):
- self.y = y
- def method_parent2(self):
- print(f"Parent2 method, y = {self.y}")
- class Child(Parent1, Parent2):
- def __init__(self, x, y):
- super().__init__(x)
- self.y = y
- def method_child(self):
- print("Child method")
- # Function to print divider lines
- def line():
- print("-" * 91)
- # Instantiate Child with arguments representing inheritance
- obj = Child(253, "Multiple Inheritance Demo")
- # Output explanation
- line()
- print("\n\t\t\t\t:: Output with explanation ::\n")
- line()
- # Explanation for calling method_parent2 from Parent2 class
- print("Calling method_parent2 from Parent2 class...\n")
- print("\t\t\t[Parent2 method, y = 'Multiple Inheritance']\n")
- print("- The method_parent2 method from the Parent2 class was called successfully.")
- print("- It prints the value of the attribute 'y' which is passed during initialization of Child.\n")
- line()
- # Explanation for calling method_child from Child class
- print("Calling method_child from Child class...\n")
- print("\t\t\t[Child method]\n")
- print("- This confirms that the method_child method from the Child class was called successfully.")
- print("- Proof that the Child class can define its own methods, independent of its parent classes.\n")
- line()
- # Explanation for isinstance checks
- print("\n\t\t\t\t:: Isinstance checks ::\n")
- line()
- print("Calling isinstance checks...\n")
- print("\tisinstance(obj, Parent1):", isinstance(obj, Parent1))
- print("\tisinstance(obj, Parent2):", isinstance(obj, Parent2))
- print("\tisinstance(obj, Child): ", isinstance(obj, Child))
- print("\n- These checks confirm the inheritance relationships between classes.\n")
- line()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement