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 4 · Memory, Profiling, Optimizationdataclass slots and kw_onlypredict52 / 161
+100 XP
Task
📝 **Question:** Predict the exact output. Two flags in action: \`Point\` uses \`slots=True\`, \`User\` uses \`kw_only=True\`. Two things to predict: (1) does \`p.z = 99\` succeed on a slotted Point? (2) does \`User("Ann", 30)\` work, or does kw_only block positional args? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

from dataclasses import dataclass


@dataclass(slots=True)
class Point:
    x: float
    y: float


@dataclass(kw_only=True)
class User:
    name: str
    age: int = 0


p = Point(1.0, 2.0)
print(f"p:{p.x},{p.y}")

try:
    p.z = 99
    print("z_set")
except AttributeError:
    print("p:slots_block")

try:
    User("Ann", 30)
except TypeError:
    print("user:positional_blocked")

u = User(name="Ann", age=30)
print(f"u:{u.name},{u.age}")

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…