-
Notifications
You must be signed in to change notification settings - Fork 4k
[Term Entry] Python Inheritance: Hybrid Inheritance #7099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8a3a328
73c1790
7ac9fed
8d8c0f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,116 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Title: 'Hybrid Inheritance' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Description: 'Hybrid inheritance mixes inheritance types, letting a class inherit traits from multiple parents to model complex real-world ties.' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Subjects: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'AI' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Computer Science' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Data Science' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Machine Learning' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Tags: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'AI' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Inheritance' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Machine Learning' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'Python' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CatalogContent: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'learn-python-3' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 'paths/computer-science' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
**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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## Diagram | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The following diagram shows a hybrid inheritance structure where a class inherits from multiple parent classes, combining multilevel and multiple inheritance: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
System | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/ \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/ \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Database API | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\ / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\ / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
App | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `System`: Base class with general functionality. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `Database`: Inherits from `System`, adds data storage capabilities. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `API`: Inherits from `System`, adds request handling capabilities. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `App`: Inherits from both `Database` and `API`, combining their features. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+21
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have a pictorial representation here? Example: https://www.codecademy.com/article/what-is-python-inheritance#heading-types-of-inheritance-in-python |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## Syntax | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Base class attributes and methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class BaseClass: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Inherits from BaseClass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class DerivedClass1(BaseClass): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Inherits from BaseClass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class DerivedClass2(BaseClass): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Inherits from DerivedClass1 and DerivedClass2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class HybridClass(DerivedClass1, DerivedClass2): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `BaseClass`: The top-level parent class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `DerivedClass1`, `DerivedClass2`: Intermediate classes inheriting from `BaseClass`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- `HybridClass`: The class combining inheritance from multiple parent classes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
- 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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be added in as a note |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class System: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def process(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Processing data" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Database(System): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def store(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Storing data" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class API(System): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def request(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Handling request" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class App(Database, API): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def describe(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return f"{self.process()}, {self.store()}, {self.request()}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app = App() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(app.describe()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+67
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation should be two spaces |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The output would be: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Processing data, Storing data, Handling request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## Codebyte | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```codebyte/python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class System: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def process(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Processing data" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Database(System): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def store(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Storing data" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class API(System): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def request(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Handling request" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class App(Database, API): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def describe(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return f"{self.process()}, {self.store()}, {self.request()}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app = App() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(app.describe()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+96
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.