-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[class-inheritance] Concept Documentation #2960
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
Changes from 30 commits
3d68dcc
c73856a
2d65f17
38b2631
4c11958
f3c8bee
df00240
c272ced
54701e4
ef69e5f
6fd43ee
fe2ccb7
1244a82
a493c91
a05b2b9
5e88b46
0339372
9ffe896
e0190c7
87cbb76
d907168
c3c2edc
55a3163
0bb7ca6
18321f5
7fd20ab
2dfbb1d
8dfc81b
3ca94b7
36064a3
5ea681a
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "blurb": "TODO: add blurb for this concept", | ||
| "authors": ["bethanyg", "cmccandless"], | ||
| "contributors": [] | ||
| "blurb": "In Object Oriented Programming ('OOP'), inheritance refers to a class's capacity to copy or 'inherit' properties and methods from another class. Python allows inheritance from a single class or multiple classes. Existing classes are known as base (or parent) classes, and new classes are known as derived (or child) classes.", | ||
| "authors": ["girijakar", "bethanyg"], | ||
| "contributors": [ ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,150 @@ | ||
| #TODO: Add about for this concept. | ||
| # About | ||
|
|
||
| Inheritance is one of the ['four pillars'][four-pillars] of Object Oriented Programming (`OOP`). | ||
| In situations where only a small amount of functionality needs to be customized for a new class, `inheritance` allows code re-use from one or more parent classes, and can help make programs cleaner and more maintainable. | ||
|
|
||
| ## Inheritance | ||
|
|
||
| `Inheritance` describes `is a kind of` relationship between two or more classes, abstracting common details into super class and storing specific ones in the subclass. | ||
| To create a child class, specify the parent class name inside the pair of parenthesis, followed by it's name. | ||
| Example | ||
| ```python | ||
| class Child(Parent): | ||
| pass | ||
| ``` | ||
| Every child class inherits all the behaviours exhibited by their parent class. | ||
BethanyG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Single Inheritance | ||
|
|
||
| When a derived (or child) class inherits only from one base (or parent) class, it is known as single inheritance. | ||
BethanyG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```python | ||
| class Person: | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def __init__(self, fname, lname): | ||
| self.fname = fname | ||
| self.lname = lname | ||
| class Employee(Person): | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| all_employees = [] | ||
| def __init__(self, fname, lname, empid): | ||
| Person.__init__(self, fname, lname) | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.empid = empid | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Employee.all_employees.append(self) | ||
| ``` | ||
| `Employee` class is derived from `Person`. | ||
| Now, we can create an `Employee` object. | ||
BethanyG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```python | ||
| ... | ||
| p1 = Person('George', 'smith') | ||
| print(p1.fname, '-', p1.lname) | ||
| e1 = Employee('Jack', 'simmons', 456342) | ||
| e2 = Employee('John', 'williams', 123656) | ||
| print(e1.fname, '-', e1.empid) | ||
| print(e2.fname, '-', e2.empid) | ||
| ``` | ||
| After running the program we will get the following output | ||
| ```cmd | ||
BethanyG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| George - smith | ||
| Jack - 456342 | ||
| John - 123656 | ||
| ``` | ||
| ## Multiple Inheritance | ||
| As we've seen, a class inherits from another class in this scenario. On the other side, multiple inheritance is a feature that allows a class to inherit characteristics and methods from many parent classes. | ||
BethanyG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ```python | ||
| class SubclassName(BaseClass1, BaseClass2, ...): | ||
| pass | ||
| ``` | ||
|
Member
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. I think this section needs to be extended to talk about how multiple inheritance works -- and how the parent classes are read in. I think it might also be helpful to limit the initial example to only two base classes - one the "core" class, and one the "secondary", to help lead into the diamond problem and then to why mixins are a thing. As with the single inheritance section above, I think it's important here to show how class attributes, instance attributes, and methods are inherited, and from there, how those get extended or overridden in the child class, so more code examples, and more narrative. |
||
| ### Diamond Problem | ||
BethanyG marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| The "diamond problem" (also known as the "deadly diamond of death") refers to an ambiguity that occurs when two classes B and C inherit from a superclass A, while another class D inherits from both B and C. If A has a method "m" that B or C (or even both of them) has overridden, and if it does not override this method, the question becomes which version of the method D inherits. It's possible that it's from A, B, or C. | ||
| Let's have a look at the problem using an example: | ||
|
|
||
| ```python | ||
| class A: | ||
| def m(self): | ||
| print("m of A called") | ||
| class B(A): | ||
| def m(self): | ||
| print("m of B called") | ||
| class C(A): | ||
| def m(self): | ||
| print("m of C called") | ||
| class D(B,C): | ||
| pass | ||
| ``` | ||
| If we call an instance x of class D, we will get the output as `m of B called`. But if we interchange the order of inheritance in class D i.e. `Class D(C, D)`. We will get the output as `m of C called`. | ||
| To solve the diamond problem in python, we will look into a new method `mro()`. | ||
|
Member
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. This isn't a new method, this is how Python solves the diamond problem. |
||
| ### Method resolution order(MRO) | ||
|
|
||
| To get the method resolution order of a class we can use either `__mro__` attribute or `mro()` method. By using these methods we can display the order in which methods are resolved. For Example | ||
|
|
||
| ```python | ||
| class A: | ||
| def m(self): | ||
| print(" m of A called") | ||
| class B: | ||
| def m(self): | ||
| print(" m of B called") | ||
|
|
||
| # classes ordering | ||
| class C(A, B): | ||
| def __init__(self): | ||
| print("Constructor C") | ||
|
|
||
| r = C() | ||
|
|
||
| # it prints the lookup order | ||
| print(C.__mro__) | ||
| print(C.mro()) | ||
| ``` | ||
| The output | ||
| ```cmd | ||
| Constructor C | ||
| (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>) | ||
| [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>] | ||
| ``` | ||
| ### Mixins | ||
| A mixin is a type of multiple inheritance that is unique. Mixins are typically employed in one of two scenarios: | ||
|
|
||
| 1. We wish to give a class a number of optional features. | ||
| 1. We want to use a specific feature in a variety of classes. | ||
|
|
||
| For example | ||
| ```python | ||
| class A1(object): | ||
| def method(self): | ||
| return 1 | ||
|
|
||
| class A2(object): | ||
| def method(self): | ||
| return 2 | ||
|
|
||
| class B1(object): | ||
| def usesMethod(self): | ||
| return self.method() + 10 | ||
|
|
||
| class B2(object): | ||
| def usesMethod(self): | ||
| return self.method() + 20 | ||
|
|
||
| class C1_10(A1, B1): pass | ||
| class C1_20(A1, B2): pass | ||
| class C2_10(A2, B1): pass | ||
| class C2_20(A2, B2): pass | ||
| ``` | ||
| Mixins helps us to recombine functionalities with different choices of base classes. | ||
| #### Pros and Cons of Mixins | ||
| | Advantages | Disadvantages | | ||
| |:-- | :-- | | ||
| |Mixin classes tend to be simple because they represent simple orthogonal concepts. | Execution of statements at run time tends to jump around in different mixins, making it hard to follow and debug| | ||
| |Helps us to recombine functionalities with different choices | Potential for long compile times| | ||
| ## __super()__ | ||
| In a nutshell, `super()` gives us access to methods in a superclass from the subclass that inherits from it. | ||
| `super()` by itself returns a temporary object of the superclass, which may subsequently be used to call the methods of that superclass. | ||
|
|
||
| But why we want to use `super()`? | ||
|
|
||
| Using `super()` to call already created methods avoids having to rebuild those methods in our subclass and allows us to swap out superclasses with little code modifications. | ||
girijakar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [four-pillars]: https://www.educative.io/edpresso/what-are-the-four-pillars-of-oops-in-python | ||
|
|
||
| [four-pillars]: https://www.educative.io/edpresso/what-are-the-four-pillars-of-oops-in-python | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| #TODO: Add introduction for this concept. | ||
| # Introduction | ||
|
|
||
| [Inheritance](inherit) represents what is known as a relationship. When a Derived class inherits from a Base class, you've established a relationship in which Derived is a specialised version of Base. | ||
| Either by using single or multiple inheritance, we can inherit the properties from the base class. Inheritance is used because it helps in code reusability. | ||
|
|
||
|
|
||
| [inherit]:https://realpython.com/inheritance-composition-python/#whats-inheritance |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,42 @@ | ||
| [ | ||
| { | ||
| "url": "http://example.com/", | ||
| "description": "TODO: add new link (above) and write a short description here of the resource." | ||
| "url": "https://docs.python.org/3/tutorial/classes.html", | ||
| "description": "Classes in python" | ||
| }, | ||
| { | ||
| "url": "http://example.com/", | ||
| "description": "TODO: add new link (above) and write a short description here of the resource." | ||
| "url": "https://www.geeksforgeeks.org/access-modifiers-in-python-public-private-and-protected/", | ||
| "description": "Access Modifiers in python" | ||
| }, | ||
| { | ||
| "url": "http://example.com/", | ||
| "description": "TODO: add new link (above) and write a short description here of the resource." | ||
| "url": "https://www.geeksforgeeks.org/accessor-and-mutator-methods-in-python/", | ||
| "description": "Functions in classes" | ||
| }, | ||
| { | ||
| "url": "http://example.com/", | ||
| "description": "TODO: add new link (above) and write a short description here of the resource." | ||
| "url": "https://realpython.com/inheritance-composition-python/#whats-inheritance", | ||
| "description": "About inheritance" | ||
| }, | ||
| { | ||
| "url": "https://python-course.eu/oop/multiple-inheritance.php", | ||
| "description": "Multiple inheritance and Diamond inheritance problem" | ||
| }, | ||
| { | ||
| "url": "https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful", | ||
| "description": "Python mixins" | ||
| }, | ||
| { | ||
| "url": "https://raganwald.com/2016/07/16/why-are-mixins-considered-harmful.html", | ||
| "description": "Mixins cons" | ||
| }, | ||
| { | ||
| "url": "https://gaopinghuang0.github.io/2018/12/29/dig-into-python-super-and-mro", | ||
| "description": "Super and mixins functions" | ||
| }, | ||
| { | ||
| "url": "https://www.python.org/download/releases/2.3/mro/", | ||
| "description": "MRO" | ||
| }, | ||
| { | ||
| "url": "https://nedbatchelder.com/blog/201210/multiple_inheritance_is_hard.html", | ||
| "description": "Multiple inheritancs and mixins" | ||
| } | ||
| ] |
Uh oh!
There was an error while loading. Please reload this page.