-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[New Entry/ Edits] Python Builtin-Functions: super()/ Inheritance
#1008
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
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
b7e1947
add definition for python super()
beverand 5b56315
Made set of updateds for spacing and content improvement
beverand 2a3868c
Removed note
beverand 4e8541e
Bold inheritance in tag list
beverand 27c8813
Revert "Bold inheritance in tag list"
beverand d10ece8
Bold Inheritance on line 16
beverand 305fa50
Removed extra line
beverand a1b91b5
Ran npm run format on super.md and inheirtance.md
beverand 6b09e1f
Merge branch 'main' into docsuper
beverand 439bf67
Merge branch 'Codecademy:main' into docsuper
beverand 7dcbaa0
Update content/python/concepts/inheritance/inheritance.md
beverand 3e96c3c
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand 6a0e845
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand 2e5d13d
Additional changes from second review update links and text changes
beverand 868517b
remove yarn.lock
beverand b5f32c8
Move text from above pseudo to below it
beverand 068529a
Merge branch 'Codecademy:main' into docsuper
beverand f910ced
add yarn.lock back
Dusch4593 7ec157c
Delete yarn.lock
Dusch4593 8a2e4a7
added yarn.lock back (again)
Dusch4593 dd4e782
Merge branch 'main' into docsuper
Dusch4593 5a7a852
re-mirrored yarn.lock against one from main branch
Dusch4593 90cfa39
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand 36eb362
Updated super(type) to super(type, object) with example
beverand 208a536
Update reference to 'object' in pseudocode to 'obj'
beverand 6229e9a
remove line at 73
beverand ad696a8
Update intro to example from "this case" to "the following codebyte e…
beverand a32392f
remove line at 56
beverand 710956d
Shorten reference from "object" to "obj"
beverand c3ea348
Change example intro at line 39
beverand 72352dc
Remove Codebyte from example
beverand 1e1e5e8
Reverse change from codebyte/python back to py
beverand 318bfe5
Clarify child and subclass references
beverand 983e419
Update to note add >
beverand e477f1a
Add 'Classes' to tags
beverand 2054f70
Updates including mod to codebyte example and additional pseudo code
beverand 9dd45cb
some last edits
Dusch4593 d56e8a2
Merge branch 'main' into docsuper
Dusch4593 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
content/python/concepts/built-in-functions/terms/super/super.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| Title: 'super()' | ||
| Description: 'Returns a temporary object that allows a given class to inherit the methods and properties of a parent or sibling class.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Data Science' | ||
| Tags: | ||
| - 'Functions' | ||
| - 'Objects' | ||
| - 'Inheritance' | ||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - 'Classes' | ||
| - 'Functions' | ||
| - 'Inheritance' | ||
| - 'Methods' | ||
| - 'Objects' | ||
| - 'OOP' | ||
| CatalogContent: | ||
| - 'learn-python-3' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`super()`** function returns a temporary object that allows a given [class](https://www.codecademy.com/resources/docs/python/classes) to [inherit](https://www.codecademy.com/resources/docs/python/inheritance) the methods and properties of a parent or sibling class. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| super(type, obj) | ||
| ``` | ||
|
|
||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The `type` parameter specifies the type object of the parent class. The `obj` is optional and is an instance, or subtype, of the class `type`. | ||
|
|
||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Inside Class Definition | ||
|
|
||
| When used inside a class definition, the `super()` function can be used with zero arguments since the given class inherits from a parent class: | ||
|
|
||
| ```pseudo | ||
| class A(): | ||
| method(self, arg): | ||
| #Method code starts here | ||
|
|
||
| class B(A): | ||
| method(self, arg): | ||
| super().method(arg) | ||
| #Method code starts here | ||
| ``` | ||
|
|
||
| The `super()` function allows the child class `B` to access the `.method()` of parent class `A`. | ||
|
|
||
| ### Outside Class Definition | ||
|
|
||
| The following syntax can be applied inside and outside of a class definition: | ||
|
|
||
| ```pseudo | ||
| class B(A): | ||
| method(self, arg): | ||
| super().method(arg) | ||
| #Method code starts here | ||
|
|
||
| instance_of_b = B() | ||
| super(B, instance_of_b).method() | ||
| ``` | ||
|
|
||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The `super()` function accepts the class type `B` along with an `instance_of_b` variable to direct the lookup search for a `.method()` in the nearest parent class. | ||
|
|
||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Codebyte Example | ||
|
|
||
| In the following example, both syntaxes of `super()` are implemented in the classes `Python` and `Java` to access the `intro()` method of the parent `ProgramLanguage` class: | ||
|
|
||
| ```codebyte/python | ||
| # Base class | ||
| class ProgramLanguage: | ||
| def intro(self): | ||
| print("Hi! I am a Programming Language.") | ||
|
|
||
| # Derived class | ||
| class Python(ProgramLanguage): | ||
| def intro(self): | ||
| super().intro() | ||
| print("Python here!") | ||
|
|
||
| # Derived class | ||
| class Java(ProgramLanguage): | ||
| def intro(self): | ||
| super(Java, self).intro() | ||
| print("Java here!") | ||
|
|
||
| python_lang = Python() | ||
| python_lang.intro() | ||
|
|
||
beverand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| java_lang = Java() | ||
| java_lang.intro() | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.