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 2 · CPython Internals & PerformanceSlots optimization in depthpredict18 / 161
+100 XP
Task
📝 **Question:** Predict the exact output. \`Point\` declares \`__slots__ = ("x", "y")\`. \`Tagged\` subclasses \`Point\` but does NOT declare its own \`__slots__\`. Three questions: (1) Does \`Point\` instance have a \`__dict__\`? (2) Does \`Tagged\` instance have one? (3) Can you set \`p.z = 99\` and \`t.z = 99\`? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

class Point:
    __slots__ = ("x", "y")
    def __init__(self, x, y):
        self.x = x
        self.y = y


class Tagged(Point):
    pass


p = Point(1, 2)
t = Tagged(3, 4)

print(f"p:{p.x},{p.y}")
print(f"t:{t.x},{t.y}")
print(hasattr(p, "__dict__"))
print(hasattr(t, "__dict__"))

try:
    p.z = 99
    print(f"p.z={p.z}")
except AttributeError:
    print("p:no_z")

t.z = 99
print(f"t.z={t.z}")

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…