|
| 1 | +--- |
| 2 | +Title: 'Hybrid Inheritance' |
| 3 | +Description: 'Hybrid inheritance combines multiple types of inheritance, allowing a class to inherit features from more than one parent and represent complex real-world relationships.' |
| 4 | +Subjects: |
| 5 | + - 'AI' |
| 6 | + - 'Computer Science' |
| 7 | + - 'Data Science' |
| 8 | + - 'Machine Learning' |
| 9 | +Tags: |
| 10 | + - 'AI' |
| 11 | + - 'Inheritance' |
| 12 | + - 'Machine Learning' |
| 13 | + - 'Python' |
| 14 | +CatalogContent: |
| 15 | + - 'learn-python-3' |
| 16 | + - 'paths/computer-science' |
| 17 | +--- |
| 18 | + |
| 19 | +**Hybrid inheritance** in Python combines multiple types of inheritance, such as single, multiple, and multilevel inheritance, within a single class hierarchy. It allows a class to inherit attributes and methods from multiple parent classes, often through a complex structure. Hybrid inheritance is useful for modeling real-world relationships where a class needs behaviors from various sources, but it requires careful design to avoid ambiguity, such as the diamond problem. |
| 20 | + |
| 21 | +## Diagram |
| 22 | + |
| 23 | +The following diagram shows a hybrid inheritance structure where a class inherits from multiple parent classes, combining multilevel and multiple inheritance: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +- `System`: Base class with general functionality. |
| 28 | +- `Database`: Inherits from `System`, adds data storage capabilities. |
| 29 | +- `API`: Inherits from `System`, adds request handling capabilities. |
| 30 | +- `App`: Inherits from both `Database` and `API`, combining their features. |
| 31 | + |
| 32 | +## Syntax |
| 33 | + |
| 34 | +```python |
| 35 | +# Base class attributes and methods |
| 36 | +class BaseClass: |
| 37 | + |
| 38 | +# Inherits from BaseClass |
| 39 | +class DerivedClass1(BaseClass): |
| 40 | + |
| 41 | +# Inherits from BaseClass |
| 42 | +class DerivedClass2(BaseClass): |
| 43 | + |
| 44 | +# Inherits from DerivedClass1 and DerivedClass2 |
| 45 | +class HybridClass(DerivedClass1, DerivedClass2): |
| 46 | +``` |
| 47 | + |
| 48 | +- `BaseClass`: The top-level parent class. |
| 49 | +- `DerivedClass1`, `DerivedClass2`: Intermediate classes inheriting from `BaseClass`. |
| 50 | +- `HybridClass`: Inherits from both intermediate classes, forming a hybrid structure (mix of multilevel and multiple inheritance). |
| 51 | + |
| 52 | +> **Note:** Use commas in the class definition to specify multiple parent classes. Python’s Method Resolution Order (MRO) determines which parent class method is called in case of conflicts. |
| 53 | +
|
| 54 | +## Example |
| 55 | + |
| 56 | +This example defines an `System` base class with a `process` method. `Database` and `API` inherit from `System`, adding `store` and `request` methods, respectively. `App` uses hybrid inheritance to inherit from both `Database` and `API`, combining their behaviors. The `describe` method in `App` calls methods from all parent classes, demonstrating access to inherited functionality. |
| 57 | + |
| 58 | +```py |
| 59 | +class System: |
| 60 | + def process(self): |
| 61 | + return "Processing data" |
| 62 | + |
| 63 | +class Database(System): |
| 64 | + def store(self): |
| 65 | + return "Storing data" |
| 66 | + |
| 67 | +class API(System): |
| 68 | + def request(self): |
| 69 | + return "Handling request" |
| 70 | + |
| 71 | +class App(Database, API): |
| 72 | + def describe(self): |
| 73 | + return f"{self.process()}, {self.store()}, {self.request()}" |
| 74 | + |
| 75 | +app = App() |
| 76 | +print(app.describe()) |
| 77 | +``` |
| 78 | + |
| 79 | +The output would be: |
| 80 | + |
| 81 | +```shell |
| 82 | +Processing data, Storing data, Handling request |
| 83 | +``` |
| 84 | + |
| 85 | +## Codebyte |
| 86 | + |
| 87 | +```codebyte/python |
| 88 | +class System: |
| 89 | + def process(self): |
| 90 | + return "Processing data" |
| 91 | +
|
| 92 | +class Database(System): |
| 93 | + def store(self): |
| 94 | + return "Storing data" |
| 95 | +
|
| 96 | +class API(System): |
| 97 | + def request(self): |
| 98 | + return "Handling request" |
| 99 | +
|
| 100 | +class App(Database, API): |
| 101 | + def describe(self): |
| 102 | + return f"{self.process()}, {self.store()}, {self.request()}" |
| 103 | +
|
| 104 | +app = App() |
| 105 | +print(app.describe()) |
| 106 | +``` |
| 107 | + |
| 108 | +Hybrid inheritance can lead to the diamond problem, where a class inherits the same method from multiple parents. Python resolves this using the Method Resolution Order (MRO), accessible via **`ClassName.mro()`**. Use hybrid inheritance judiciously to avoid complex hierarchies that are hard to maintain. Ensure parent classes are designed to work together to prevent method conflicts. |
0 commit comments