Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 7 · Memory, GC, modern typing__match_args__ — making your classes destructurablepredict124 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. Five Color values are matched against five cases in order. Literal exact match, binding + guard, and wildcard-with-literal each exercise a different shape. The first matching case wins for each input. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class Color:
    __match_args__ = ("r", "g", "b")

    def __init__(self, r: int, g: int, b: int) -> None:
        self.r, self.g, self.b = r, g, b


def describe(c: Color) -> str:
    match c:
        case Color(0, 0, 0):
            return "black"
        case Color(255, 255, 255):
            return "white"
        case Color(r, g, b) if r == g == b:
            return f"gray({r})"
        case Color(r, 0, 0):
            return f"red-only({r})"
        case Color(r, g, b):
            return f"rgb({r},{g},{b})"
    return "unreachable"


print(describe(Color(0, 0, 0)))
print(describe(Color(255, 255, 255)))
print(describe(Color(50, 50, 50)))
print(describe(Color(200, 0, 0)))
print(describe(Color(10, 20, 30)))

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…