Question: python class Course: def __init__(self, students: list[Student]): self._students = students def students(self) -> list[Student]: return self._students def add_student(self, student: Student) -> None: self._students.append(student) In particular,

python class Course: def __init__(self, students: list[Student]): self._students = students def students(self) -> list[Student]: return self._students def add_student(self, student: Student) -> None: self._students.append(student) 

In particular, we say that a course is a collection of students, that we can find out what students are enrolled in that course, and that we can add a new student to the course, but that there is no way for a student to drop. (Of course, there are plenty of other things that we might like to be able to do, but let's stick with these few features for now.)

Now let's consider the impact of mutability on this design.

  1. Are objects of our Course class mutable or immutable? In a sentence or so, briefly explain why.
  2. The __init__ method accepts a parameter that is a list of Student objects, which we store directly in an attribute within self. Given that lists are mutable, does this pose any design risks (i.e., ways for our program to behave unpredictably even if no code in the Course class is incorrect)? If so, are there ways to mitigate those risks?
  3. The students method returns the list of Student objects stored within a Course. Given that lists are mutable, does this pose any design risks (i.e., ways for our program to behave unpredictably even if no code in the Course class is incorrect)? If so, are there ways to mitigate those risks?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!