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 & PerformanceWeak references (weakref module)predict19 / 161
+100 XP
Task
📝 **Question:** Predict the exact output. \`obj = Big("A")\` is the only strong reference; \`ref\` is a weak reference. After \`del obj\` + \`gc.collect()\`, what does \`ref()\` return? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

import weakref
import gc


class Big:
    def __init__(self, label):
        self.label = label


obj = Big("A")
ref = weakref.ref(obj)
print(f"alive:{ref() is not None}")
print(f"label:{ref().label}")
del obj
gc.collect()
print(f"alive:{ref() is not None}")
print(f"deref:{ref()}")

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…