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@dataclass(frozen=True) + dataclasses.replace — immutable updatespredict122 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. The frozen Point can't be mutated directly; `replace` returns a NEW instance with the named overrides; equality is by value, hashing is consistent with equality. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

from dataclasses import dataclass, replace, FrozenInstanceError


@dataclass(frozen=True)
class Point:
    x: int
    y: int


p = Point(1, 2)
print(p)

try:
    p.x = 99
except FrozenInstanceError:
    print("frozen")

p2 = replace(p, y=20)
print(p2)
print(p is p2)
print(p == Point(1, 2))
print(hash(p) == hash(Point(1, 2)))

p3 = replace(p, x=100, y=200)
print(p3)

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…