Implement Director awards with Python OOP
Python OOP Exercise: Director and Cast Hierarchy
Context
You are tasked with implementing a robust and testable Python solution for a simple class hierarchy where Director extends Cast. The solution must correctly utilize @property, @classmethod, and @staticmethod, and avoid common pitfalls such as mutable default arguments and leaking internal mutable state.
Here is the provided starting point for the class structure:
class Cast:
def __init__(self, actors: list[str]):
self.actors = list(actors)
class Director(Cast):
def __init__(self, name: str, actors: list[str], movies: list[str]):
super().__init__(actors)
self.name = name
self.movies = list(movies)
self._awards: list[tuple[str,int]] = [] # (award_name, year)
Tasks
- Implement
add_award(award_name: str, year: int)that appends a new(award_name, year)pair only if it does not already exist for this director (idempotent). Ensure no mutable-default arguments are used and thatself._awardscannot be mutated from outside the class. - Add a property
awardsthat returns an immutable, chronologically sorted view of awards. Add another propertylatest_awardthat returnsOptional[tuple[str,int]]. - Add a
@staticmethod validate_award(award_name: str, year: int)that raisesValueErrorfor empty award names or years outside the range[1900, current_year]. This static method must be used internally withinadd_award. - Add a
@classmethod from_filmography(cls, name: str, filmography: dict)that constructs aDirectorinstance from a dictionary with the structure{'actors': [...], 'movies': [...]}. Demonstrate how subclassing would work if you were to later create anAwardWinningDirectorthat adds additional fields likeguild_membership. - Implement
__eq__and__hash__dunder methods such that twoDirectorinstances with the samenamecompare equal. Explain the trade-offs of this design choice. - Provide a short doctest-style example demonstrating the creation of a
Directorinstance, adding duplicate awards, and accessing thelatest_awardproperty.
AI interview prep
Core dilemma
Ensuring robust data integrity and strict encapsulation within a flexible Python OOP hierarchy, correctly applying language features while avoiding common pitfalls.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement Director awards with Python OOP
Python OOP Exercise: Director and Cast Hierarchy
Context
You are tasked with implementing a robust and testable Python solution for a simple class hierarchy where Director extends Cast. The solution must correctly utilize @property, @classmethod, and @staticmethod, and avoid common pitfalls such as mutable default arguments and leaking internal mutable state.
Here is the provided starting point for the class structure:
class Cast:
def __init__(self, actors: list[str]):
self.actors = list(actors)
class Director(Cast):
def __init__(self, name: str, actors: list[str], movies: list[str]):
super().__init__(actors)
self.name = name
self.movies = list(movies)
self._awards: list[tuple[str,int]] = [] # (award_name, year)
Tasks
- Implement
add_award(award_name: str, year: int)that appends a new(award_name, year)pair only if it does not already exist for this director (idempotent). Ensure no mutable-default arguments are used and thatself._awardscannot be mutated from outside the class. - Add a property
awardsthat returns an immutable, chronologically sorted view of awards. Add another propertylatest_awardthat returnsOptional[tuple[str,int]]. - Add a
@staticmethod validate_award(award_name: str, year: int)that raisesValueErrorfor empty award names or years outside the range[1900, current_year]. This static method must be used internally withinadd_award. - Add a
@classmethod from_filmography(cls, name: str, filmography: dict)that constructs aDirectorinstance from a dictionary with the structure{'actors': [...], 'movies': [...]}. Demonstrate how subclassing would work if you were to later create anAwardWinningDirectorthat adds additional fields likeguild_membership. - Implement
__eq__and__hash__dunder methods such that twoDirectorinstances with the samenamecompare equal. Explain the trade-offs of this design choice. - Provide a short doctest-style example demonstrating the creation of a
Directorinstance, adding duplicate awards, and accessing thelatest_awardproperty.