Python OOP Exercise: Director extends Cast
Context
You are working with a simple class hierarchy where Director extends Cast. Implement a robust, testable Python solution that uses @property, @classmethod, and @staticmethod correctly, and avoids common pitfalls such as mutable defaults and leaking internal mutable state.
Starting point:
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 and that self._awards cannot be mutated from outside the class.
-
Add a property awards that returns an immutable, chronologically sorted view of awards; add a property latest_award that returns Optional[tuple[str,int]].
-
Add a @staticmethod validate_award(award_name: str, year: int) that raises ValueError for empty names or years outside [1900, current_year]. Use it inside add_award.
-
Add a @classmethod from_filmography(cls, name: str, filmography: dict) that constructs a Director from {'actors': [...], 'movies': [...]}. Show how subclassing would work if you later create an AwardWinningDirector that adds fields like guild_membership.
-
Implement
eq
and
hash
so that two Director instances with the same name compare equal. Explain the trade-offs of this choice.
-
Provide a short doctest-style example demonstrating creation, adding duplicate awards, and the latest_award property.