Python: Which practice avoids a common mistake with weakref and Object Lifetime?

Difficulty:

Medium

Questions:

1

Time Limit:

2 minutes

Passing Score:

100%

Question

Which practice avoids a common mistake with weakref and Object Lifetime?

  1. Ignore the weakref and Object Lifetime issue and rely on team discipline instead of clearer APIs or invariants.
  2. Do not treat weakref as a general replacement for explicit ownership because disappearing entries change program behavior and must be expected.
  3. Silence the weakref and Object Lifetime problem by using broad catches, hidden globals, or extra shared mutable state.
  4. Prefer the version of weakref and Object Lifetime that makes behavior less predictable as long as the code still runs.

Hint

Look for the option that protects correctness instead of hiding the problem.

Answer and rationale

Correct answer: B. Do not treat weakref as a general replacement for explicit ownership because disappearing entries change program behavior and must be expected.

Do not treat weakref as a general replacement for explicit ownership because disappearing entries change program behavior and must be expected. This is a common failure mode in real Python code and a frequent interview follow-up.

Track: Python