|
| 1 | + |
| 2 | + |
| 3 | +class Parent: |
| 4 | + def __init__(self, first_name: str, last_name: str): |
| 5 | + self.first_name = first_name |
| 6 | + self.last_name = last_name |
| 7 | + |
| 8 | + def get_name(self) -> str: |
| 9 | + return f"{self.first_name} {self.last_name}" |
| 10 | + |
| 11 | + |
| 12 | +class Child(Parent): |
| 13 | + def __init__(self, first_name: str, last_name: str): |
| 14 | + super().__init__(first_name, last_name) |
| 15 | + self.previous_last_names = [] |
| 16 | + |
| 17 | + def change_last_name(self, last_name) -> None: |
| 18 | + self.previous_last_names.append(self.last_name) |
| 19 | + self.last_name = last_name |
| 20 | + |
| 21 | + def get_full_name(self) -> str: |
| 22 | + suffix = "" |
| 23 | + if len(self.previous_last_names) > 0: |
| 24 | + suffix = f" (née {self.previous_last_names[0]})" |
| 25 | + return f"{self.first_name} {self.last_name}{suffix}" |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +person1 = Child("Elizaveta", "Alekseeva") |
| 30 | +# Predict: Creates a Child object. Works perfectly. |
| 31 | + |
| 32 | +print(person1.get_name()) |
| 33 | +# Predict: it will print "Elizaveta Alekseeva" |
| 34 | +# Child inherits this method from Parent |
| 35 | + |
| 36 | +print(person1.get_full_name()) |
| 37 | +# Predict: it will print "Elizaveta Alekseeva" because the last name has not been changed yet. |
| 38 | +# get_full_name is defined in Child, but it uses the last name from Parent, which is "Alekseeva" at this point. |
| 39 | + |
| 40 | +person1.change_last_name("Tyurina") |
| 41 | +# Predict: Changes last_name to "Tyurina" and saves "Alekseeva" in the |
| 42 | +# previous_last_names list. Works perfectly. |
| 43 | + |
| 44 | +print(person1.get_name()) |
| 45 | +# Predict: Prints "Elizaveta Tyurina". |
| 46 | +# get_name is inherited from Parent, but it uses the updated last name "Tyurina". |
| 47 | + |
| 48 | +print(person1.get_full_name()) |
| 49 | +# Predict: Prints "Elizaveta Tyurina (née Alekseeva)". |
| 50 | +# get_full_name uses the updated last name "Tyurina" and also includes the previous last name "Alekseeva" in the output. |
| 51 | +print() |
| 52 | + |
| 53 | +person2 = Parent("Elizaveta", "Alekseeva") |
| 54 | +# Predict: Creates a Parent object. Works perfectly. |
| 55 | + |
| 56 | +print(person2.get_name()) |
| 57 | +# Predict: it will print "Elizaveta Alekseeva" |
| 58 | +# get_name is defined in Parent, so it works perfectly. |
| 59 | + |
| 60 | +print(person2.get_full_name()) |
| 61 | +# Predict: it will raise an AttributeError because get_full_name is not defined in Parent, |
| 62 | +# and Parent does not have access to the methods of Child. |
| 63 | + |
| 64 | +person2.change_last_name("Tyurina") |
| 65 | +# Predict: it will raise an AttributeError because change_last_name is not defined in Parent, |
| 66 | +# and Parent does not have access to the methods of Child. |
| 67 | + |
| 68 | +print(person2.get_name()) |
| 69 | +# Predict: it will print "Elizaveta Alekseeva" because the last name has not been changed due to the previous error. |
| 70 | + |
| 71 | +print(person2.get_full_name()) |
| 72 | +# Predict: it will raise an AttributeError again for the same reason as before. |
0 commit comments