Skip to content
Merged
Show file tree
Hide file tree
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 Sep 16, 2022
5b56315
Made set of updateds for spacing and content improvement
beverand Sep 16, 2022
2a3868c
Removed note
beverand Sep 16, 2022
4e8541e
Bold inheritance in tag list
beverand Sep 16, 2022
27c8813
Revert "Bold inheritance in tag list"
beverand Sep 16, 2022
d10ece8
Bold Inheritance on line 16
beverand Sep 16, 2022
305fa50
Removed extra line
beverand Sep 16, 2022
a1b91b5
Ran npm run format on super.md and inheirtance.md
beverand Sep 16, 2022
6b09e1f
Merge branch 'main' into docsuper
beverand Sep 19, 2022
439bf67
Merge branch 'Codecademy:main' into docsuper
beverand Sep 21, 2022
7dcbaa0
Update content/python/concepts/inheritance/inheritance.md
beverand Sep 21, 2022
3e96c3c
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand Sep 21, 2022
6a0e845
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand Sep 21, 2022
2e5d13d
Additional changes from second review update links and text changes
beverand Sep 21, 2022
868517b
remove yarn.lock
beverand Sep 21, 2022
b5f32c8
Move text from above pseudo to below it
beverand Sep 21, 2022
068529a
Merge branch 'Codecademy:main' into docsuper
beverand Sep 21, 2022
f910ced
add yarn.lock back
Dusch4593 Sep 22, 2022
7ec157c
Delete yarn.lock
Dusch4593 Sep 22, 2022
8a2e4a7
added yarn.lock back (again)
Dusch4593 Sep 22, 2022
dd4e782
Merge branch 'main' into docsuper
Dusch4593 Sep 22, 2022
5a7a852
re-mirrored yarn.lock against one from main branch
Dusch4593 Sep 22, 2022
90cfa39
Update content/python/concepts/built-in-functions/terms/super/super.md
beverand Sep 22, 2022
36eb362
Updated super(type) to super(type, object) with example
beverand Sep 22, 2022
208a536
Update reference to 'object' in pseudocode to 'obj'
beverand Sep 23, 2022
6229e9a
remove line at 73
beverand Sep 23, 2022
ad696a8
Update intro to example from "this case" to "the following codebyte e…
beverand Sep 23, 2022
a32392f
remove line at 56
beverand Sep 23, 2022
710956d
Shorten reference from "object" to "obj"
beverand Sep 23, 2022
c3ea348
Change example intro at line 39
beverand Sep 23, 2022
72352dc
Remove Codebyte from example
beverand Sep 23, 2022
1e1e5e8
Reverse change from codebyte/python back to py
beverand Sep 23, 2022
318bfe5
Clarify child and subclass references
beverand Sep 23, 2022
983e419
Update to note add >
beverand Sep 23, 2022
e477f1a
Add 'Classes' to tags
beverand Sep 23, 2022
2054f70
Updates including mod to codebyte example and additional pseudo code
beverand Sep 23, 2022
9dd45cb
some last edits
Dusch4593 Sep 26, 2022
d56e8a2
Merge branch 'main' into docsuper
Dusch4593 Sep 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions content/python/concepts/built-in-functions/terms/super/super.md
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'
- '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)
```

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`.

### 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()
```

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.

## 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()

java_lang = Java()
java_lang.intro()
```
28 changes: 3 additions & 25 deletions content/python/concepts/inheritance/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Subjects:
Tags:
- 'Inheritance'
- 'OOP'
- 'Classes'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

Inheritance is an object-oriented programming concept where a class (often referred to as the child class) derives attributes and behaviors from another class (often referred to as the parent class).
**Inheritance** is a concept in [object-oriented programming](https://www.codecademy.com/resources/docs/general/object-oriented-programming) where a child [class](https://www.codecademy.com/resources/docs/python/classes) (or subclass) derives attributes and behaviors from a parent or sibling class.

This eliminates the need to implement the methods inherited by a subclass, or child class, again. In other words, it enables a child class to inherit/reuse the attributes and methods of a parent class. In terms of real-world objects, it represents an IS-A relationship.

Expand Down Expand Up @@ -66,30 +67,7 @@ Hi! I am a Programming Language.
Python here!
```

**Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method.

### `super()`

`super()` returns a temporary object of the superclass, allowing the superclass’s methods to be called. This comes in handy when using [multiple inheritance](#multiple-inheritance).

In this case, the `.say_hi()` method is implemented utilizing `super()` inside `.intro()`:

```py
class Python(ProgramLanguage):
def intro(self):
super().say_hi()
print("Python here!")

doc = Python()
doc.intro()
```

The output would be:

```shell
Hi! I am a Programming Language.
Python here!
```
> **Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method.

## Overriding Methods

Expand Down