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`__slots__` — when you're paying for `__dict__` you don't usepredict119 / 161
+150 XP
Task
📝 **Task:** Predict the exact output. The Regular class has `__dict__`; the Slotted class doesn't. Setting a new attribute is allowed on Regular and raises on Slotted. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class Regular:
    def __init__(self, x, y):
        self.x = x
        self.y = y


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

    def __init__(self, x, y):
        self.x = x
        self.y = y


r = Regular(1, 2)
s = Slotted(3, 4)

print(r.x, r.y)
print(s.x, s.y)
print(hasattr(r, "__dict__"))
print(hasattr(s, "__dict__"))

r.extra = "ok"
print(r.extra)

try:
    s.extra = "no"
except AttributeError:
    print("rejected")

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…