-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[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?
[Term Entry] Python Inheritance: Hybrid Inheritance #7099
Conversation
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.
Hey @nsmith7atafi, thank you for contributing to Codecademy Docs, the entry is nicely written! 😄
I've suggested a few changes, could you please review and modify those at your earliest convenience? Thank you! 😃
@@ -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.' |
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.
Description: 'Hybrid inheritance mixes inheritance types, letting a class inherit traits from multiple parents to model complex real-world ties.' | |
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.' |
## 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. | ||
|
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.
can we have a pictorial representation here? Example: https://www.codecademy.com/article/what-is-python-inheritance#heading-types-of-inheritance-in-python
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.
@mamtawardhani Is it acceptable to use the picture from the example link you provided and update it to match my example with using System, Database, API, and App?
|
||
- `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 comment
The reason will be displayed to describe this comment to others. Learn more.
- `HybridClass`: The class combining inheritance from multiple parent classes. | |
- `HybridClass`: Inherits from both intermediate classes, forming a hybrid structure (mix of multilevel and multiple inheritance). |
- 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. |
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.
can be added in as a note
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
```python | |
```py |
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()) |
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.
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()) | |
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()) |
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.
indentation should be two spaces
|
||
The output would be: | ||
|
||
```python |
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.
```python | |
```shell |
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()) |
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.
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()) | |
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()) |
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.
Hey @nsmith7atafi, thank you for contributing to Codecademy Docs, the entry is nicely written! 😄
I've suggested a few changes, could you please review and modify those at your earliest convenience? Thank you! 😃
…tance.png file to media
…b.com/nsmith7atafi/docs into python-inheritance-hybrid_inheritance
…hybrid_inheritance
…b.com/nsmith7atafi/docs into python-inheritance-hybrid_inheritance
Description
Issue Solved
Type of Change
Checklist
main
branch.Issues Solved
section.