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 1 · Concurrency & Async InternalsDescriptorspredict6 / 161
+100 XP
Task
📝 **Task:** Predict the exact output. The \`Tracker\` descriptor prints on bind / get / set. Three things to think about: (1) When does \`__set_name__\` run? (2) Does \`b.x = 10\` go through \`__set__\`? (3) When \`total = b.x + b.y\` runs, which calls fire and in what order? 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class Tracker:
    def __set_name__(self, owner, name):
        self.name = name
        print(f"bind:{name}")
    def __get__(self, instance, owner):
        if instance is None:
            return self
        print(f"get:{self.name}")
        return instance.__dict__.get(self.name, 0)
    def __set__(self, instance, value):
        print(f"set:{self.name}={value}")
        instance.__dict__[self.name] = value


class Box:
    x = Tracker()
    y = Tracker()


b = Box()
b.x = 10
b.y = 5
total = b.x + b.y
print(total)

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…