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 5 · Architecture & Code Review__slots__ deep divepredict69 / 161
+100 XP
Task
📝 **Question:** Predict the exact output. \`Compact\` uses \`__slots__ = ("x", "y")\` — no \`__dict__\`. \`Hybrid(Compact)\` adds \`__slots__ = ("__dict__",)\` — explicitly re-enables per-instance dict on the subclass. Three questions: (1) Can \`c.z = 99\` succeed? (2) Can \`h.z = 99\` succeed? (3) Does each instance have a \`__dict__\`? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

class Compact:
    __slots__ = ("x", "y")


class Hybrid(Compact):
    __slots__ = ("__dict__",)


c = Compact()
c.x = 1
c.y = 2
try:
    c.z = 99
    print("compact:z_ok")
except AttributeError:
    print("compact:no_z")

h = Hybrid()
h.x = 1
h.y = 2
h.z = 99
print(f"hybrid:{h.x},{h.y},{h.z}")
print(hasattr(c, "__dict__"))
print(hasattr(h, "__dict__"))

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…