Skip to content

Commit fe883b6

Browse files
authored
[New Entry/ Edits] Python Builtin-Functions: super()/ Inheritance (#1008)
* add definition for python super() * Made set of updateds for spacing and content improvement * Removed note * Bold inheritance in tag list * Revert "Bold inheritance in tag list" This reverts commit 4e8541e. * Bold Inheritance on line 16 * Removed extra line * Ran npm run format on super.md and inheirtance.md * Update content/python/concepts/inheritance/inheritance.md * Update content/python/concepts/built-in-functions/terms/super/super.md * Update content/python/concepts/built-in-functions/terms/super/super.md * Additional changes from second review update links and text changes * remove yarn.lock * Move text from above pseudo to below it * add yarn.lock back * Delete yarn.lock * added yarn.lock back (again) * re-mirrored yarn.lock against one from main branch * Update content/python/concepts/built-in-functions/terms/super/super.md * Updated super(type) to super(type, object) with example * Update reference to 'object' in pseudocode to 'obj' * remove line at 73 * Update intro to example from "this case" to "the following codebyte example" * remove line at 56 * Shorten reference from "object" to "obj" * Change example intro at line 39 * Remove Codebyte from example * Reverse change from codebyte/python back to py * Clarify child and subclass references * Update to note add > * Add 'Classes' to tags * Updates including mod to codebyte example and additional pseudo code * some last edits
1 parent 784df42 commit fe883b6

File tree

2 files changed

+95
-25
lines changed

2 files changed

+95
-25
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
Title: 'super()'
3+
Description: 'Returns a temporary object that allows a given class to inherit the methods and properties of a parent or sibling class.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Functions'
9+
- 'Objects'
10+
- 'Inheritance'
11+
- 'Classes'
12+
- 'Functions'
13+
- 'Inheritance'
14+
- 'Methods'
15+
- 'Objects'
16+
- 'OOP'
17+
CatalogContent:
18+
- 'learn-python-3'
19+
- 'paths/computer-science'
20+
---
21+
22+
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.
23+
24+
## Syntax
25+
26+
```pseudo
27+
super(type, obj)
28+
```
29+
30+
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`.
31+
32+
### Inside Class Definition
33+
34+
When used inside a class definition, the `super()` function can be used with zero arguments since the given class inherits from a parent class:
35+
36+
```pseudo
37+
class A():
38+
method(self, arg):
39+
#Method code starts here
40+
41+
class B(A):
42+
method(self, arg):
43+
super().method(arg)
44+
#Method code starts here
45+
```
46+
47+
The `super()` function allows the child class `B` to access the `.method()` of parent class `A`.
48+
49+
### Outside Class Definition
50+
51+
The following syntax can be applied inside and outside of a class definition:
52+
53+
```pseudo
54+
class B(A):
55+
method(self, arg):
56+
super().method(arg)
57+
#Method code starts here
58+
59+
instance_of_b = B()
60+
super(B, instance_of_b).method()
61+
```
62+
63+
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.
64+
65+
## Codebyte Example
66+
67+
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:
68+
69+
```codebyte/python
70+
# Base class
71+
class ProgramLanguage:
72+
def intro(self):
73+
print("Hi! I am a Programming Language.")
74+
75+
# Derived class
76+
class Python(ProgramLanguage):
77+
def intro(self):
78+
super().intro()
79+
print("Python here!")
80+
81+
# Derived class
82+
class Java(ProgramLanguage):
83+
def intro(self):
84+
super(Java, self).intro()
85+
print("Java here!")
86+
87+
python_lang = Python()
88+
python_lang.intro()
89+
90+
java_lang = Java()
91+
java_lang.intro()
92+
```

content/python/concepts/inheritance/inheritance.md

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ Subjects:
88
Tags:
99
- 'Inheritance'
1010
- 'OOP'
11+
- 'Classes'
1112
CatalogContent:
1213
- 'learn-python-3'
1314
- 'paths/computer-science'
1415
---
1516

16-
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).
17+
**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.
1718

1819
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.
1920

@@ -66,30 +67,7 @@ Hi! I am a Programming Language.
6667
Python here!
6768
```
6869

69-
**Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method.
70-
71-
### `super()`
72-
73-
`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).
74-
75-
In this case, the `.say_hi()` method is implemented utilizing `super()` inside `.intro()`:
76-
77-
```py
78-
class Python(ProgramLanguage):
79-
def intro(self):
80-
super().say_hi()
81-
print("Python here!")
82-
83-
doc = Python()
84-
doc.intro()
85-
```
86-
87-
The output would be:
88-
89-
```shell
90-
Hi! I am a Programming Language.
91-
Python here!
92-
```
70+
> **Note**: Inside the child class, the same number and type of arguments need to be passed into the parent class' method.
9371
9472
## Overriding Methods
9573

0 commit comments

Comments
 (0)