Skip to content

Commit 3da4add

Browse files
Complete exercises for Module Tools/Sprint 5/Prep - step 9 'Inheritance'
1 parent 6f60796 commit 3da4add

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

prep-exercises/i_inheritance.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Parent:
2+
def __init__(self, first_name: str, last_name: str):
3+
self.first_name = first_name
4+
self.last_name = last_name
5+
6+
def get_name(self) -> str:
7+
return f"{self.first_name} {self.last_name}"
8+
9+
10+
class Child(Parent):
11+
def __init__(self, first_name: str, last_name: str):
12+
super().__init__(first_name, last_name)
13+
self.previous_last_names = []
14+
15+
def change_last_name(self, last_name: str) -> None:
16+
self.previous_last_names.append(self.last_name)
17+
self.last_name = last_name
18+
19+
def get_full_name(self) -> str:
20+
suffix = ""
21+
if len(self.previous_last_names) > 0:
22+
suffix = f" (née {self.previous_last_names[0]})"
23+
return f"{self.first_name} {self.last_name}{suffix}"
24+
25+
person1 = Child("Elizaveta", "Alekseeva")
26+
print(person1.get_name())
27+
print(person1.get_full_name())
28+
person1.change_last_name("Tyurina")
29+
print(person1.get_name())
30+
print(person1.get_full_name())
31+
32+
person2 = Parent("Elizaveta", "Alekseeva")
33+
print(person2.get_name())
34+
print(person2.get_full_name())
35+
person2.change_last_name("Tyurina")
36+
print(person2.get_name())
37+
print(person2.get_full_name())
38+
39+
40+
# ------------------------
41+
# Exercise 1
42+
# ------------------------
43+
# Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (
44+
# If any lines cause errors, you may need to comment them out to check later lines).
45+
46+
# Line 1: Create the class Parent
47+
# Line 2: Define the constructor method (__init__) that takes self, a first_name value with a type of str, and last_name value with a type of str
48+
# Line 3: Creates the first_name field of the object
49+
# Line 4: Creates the last_name field of the object
50+
# Line 6: Define a get_name method that returns a string
51+
# Line 7: Returns a formatted string of the object's first_name and last_name
52+
# Line 10: Create the subclass Child (inherits from the Parent class)
53+
# Line 11: Define the Child class constructor method and pass in the same arguments of a first_name with a type of str, and last_name with a type of str
54+
# Line 12: Call super() to run the Parent constructor method and set up first_name and last_name
55+
# Line 13: Creates a previous_last_names field (which is unique to the Child and not seen in the Parent class) and sets it to an empty list
56+
# Line 15: Define a change_last_name method that takes a new last_name of type str and doesn't return anything as annotated by the type None
57+
# Line 16: Uses the append method to append the current last_name value to the previous_last_names list
58+
# Line 17: Update the object's last_name field with the new last_name value that was passed in
59+
# Line 19: Define a get_full_name method that returns a string type
60+
# Line 20: Creates a local variable called suffix and sets it to an empty string value
61+
# Line 21: Checks if previous_last_names list has any items (names) by checking the length (checks if it is not empty (i.e. greater than 0))
62+
# Line 22: If it does set the suffix variable to a formatted string that has the word 'née' first then the original last_name value of the previous last name (at the first position in the previous_last_name list)
63+
# Line 23: Returns and formatted string of the Child object's first_name and last_name and adds the suffix at the end
64+
65+
# For person1: I predict that we will see "Elizaveta Alekseeva" as the first print output for get.name()
66+
# For get_full_name I predict we will see just "Elizaveta Alekseeva" as there has been no other last names passed to the Child as yet
67+
# Once change_last_name has been passed a new name, I expect to see "Elizaveta Tyurina"
68+
# For get_full_name I predict we will see "Elizaveta Tyurina (née Alekseeva)"
69+
70+
# For person2: I predict that we will see "Elizaveta Alekseeva" for the get.name()
71+
# I imagine there will be an error for print(person2.get_full_name()) because the Parent does not have a get_full_name method
72+
# I predict it will fail for person2.change_last_name("Tyurina") for the same reason as the object is a parent object that has no change_last_name method
73+
# By commenting out the previous 2 lines of code ,the next get.name() will repeat the first print output
74+
# The final get_full_name output will fail again as before because this method doesn't exist in the Parent class.

0 commit comments

Comments
 (0)