Skip to content

Commit

Permalink
Fixed some styling and added a note to inheritance.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Divy1211 committed Oct 25, 2023
1 parent 46463ed commit 51079bf
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 102 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/IntroToPython.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 92 additions & 43 deletions docs/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,19 @@ class Student2:
print()

# when there are a lot of function parameters, it is nice to specify which parameters correspond to what
# values for better readability and clarity
B = Student2(name = "Robert", age = 14, sex = "male", height = 160, weight = 65, school = "SUTD", id_no = 1025, seat_no = 12, year = 1, section = "A")
# values for better readability and clarity and put them each on their own line
B = Student2(
name = "Robert",
age = 14,
sex = "male",
height = 160,
weight = 65,
school = "SUTD",
id_no = 1025,
seat_no = 12,
year = 1,
section = "A",
)
print(B.name+"'s age: "+str(B.age))
B.display_information()
```
Expand All @@ -109,7 +120,7 @@ Also, to access a student's name and age, you have to do `A.person.name` and `A.

The 2nd approach fixes this issue but it is also a bit tedious because you have to manually declare all properties of a person inside the student constructor... What if there were not 5, but 100 different properties associated with a person? It would be too unfeasable to manually rewrite them.

This is where inheritance comes into the picture. Inheritance literally allows us to "inherit" the properties of one class (called the super class) into another class (called the sub class)
This is where inheritance comes into the picture. Inheritance literally allows us to "inherit" the properties of one class (called the super or base class) into another class (called the sub or child class)

```py

Expand All @@ -132,28 +143,56 @@ class Person:
# Base/Sub class
class Student(Person):
def __init__(self, name, age, sex, height, weight, school, id_no, seat_no, year, section):
Person.__init__(self, name, age, sex, height, weight)
Person.__init__(self, name, age, sex, height, weight) # we can re-use functionality from the super class!
self.school = school
self.id_no = id_no
self.seat_no = seat_no
self.year = year
self.section = section

def display_information(self):
Person.display_information(self)
Person.display_information(self) # we can re-use functionality from the super class!
print("School : " + self.school)
print("ID : " + str(self.id_no))
print("Seat : " + str(self.seat_no))
print("Year : " + str(self.year))
print("Section : " + self.section)

# when there are a lot of function parameters, it is nice to specify which parameters correspond to what
# values for better readability and clarity
A = Student(name = "Robin", age = 16, sex = "male", height = 180, weight = 75, school = "SUTD", id_no = 1023, seat_no = 3, year = 3, section = "A")
# values for better readability and clarity and put them each on their own line
A = Student(
name = "Robin",
age = 16,
sex = "male",
height = 180,
weight = 75,
school = "SUTD",
id_no = 1023,
seat_no = 3,
year = 3,
section = "A",
)
print(A.name+"'s age: "+str(A.age))
A.display_information()
```

!!! note "Best practice"
The following usages of super class methods in the above example:
```py
Person.__init__(self, name, age, sex, height, weight)

Person.display_information(self)
```
Are for educational purposes only, in real python programs, we should make use of the following syntax instead:

```py
# notice that the self parameter has been omitted
super().__init__(name, age, sex, height, weight)

super().display_information()
```
The reason for doing so is that `super()` in python does the work of figuring out which super class's function to call and if you end up changing the superclass, you don't have to change all your code everywhere (Also there can be multiple super classes, but that's a story for another day)

=== "Practise"
Given a class computer, Write a subclass laptop and desktop with the given additional properties:

Expand Down Expand Up @@ -183,12 +222,14 @@ A.display_information()

```py
class Computer:
def __init__(self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str):
def __init__(
self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
):

# type hints can also be given to a class' data members
self.cpu: str = cpu
Expand Down Expand Up @@ -234,12 +275,14 @@ A.display_information()

```py
class Computer:
def __init__(self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str):
def __init__(
self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
):

# type hints can also be given to a class' data members
self.cpu: str = cpu
Expand All @@ -260,12 +303,14 @@ A.display_information()

```py
class Computer:
def __init__(self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str):
def __init__(
self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
):

# type hints can also be given to a class' data members
self.cpu: str = cpu
Expand All @@ -282,14 +327,16 @@ A.display_information()
print("The GPU is : "+self.gpu)

class Laptop(Computer):
def __init__(self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
resolution: str,
is_touchscreen: bool):
def __init__(
self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
resolution: str,
is_touchscreen: bool,
):
Computer.__init__(self, cpu, storage_type, storage, ram, gpu)
self.resolution = resolution
self.is_touchscreen = is_touchscreen
Expand All @@ -300,16 +347,18 @@ A.display_information()
print("Is it a touchscreen : "+str(self.is_touchscreen))

class Desktop(Computer):
def __init__(self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
monitor: str,
resolution: str,
keyboard: str,
mouse: str):
def __init__(
self,
cpu: str,
storage_type: str,
storage: float,
ram: float,
gpu: str,
monitor: str,
resolution: str,
keyboard: str,
mouse: str,
):
Computer.__init__(self, cpu, storage_type, storage, ram, gpu)
self.monitor = monitor
self.resolution = resolution
Expand Down
Loading

0 comments on commit 51079bf

Please sign in to comment.