python runtime_error official true

ReferenceError

ID: python/referenceerror

Also available as: JSON · Markdown
80%Fix Rate
95%Confidence
0Evidence

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A weak reference proxy was used after the referent was garbage collected. The object is no longer valid.

generic

Official Documentation

https://docs.python.org/3/library/exceptions.html

Workarounds

  1. 90% success Check if the weakref is still alive before accessing
    if ref() is not None: obj = ref()
  2. 90% success Use weakref.WeakMethod or WeakSet for proper weak reference management
    Let the framework manage lifecycle; access weakrefs through callbacks

Dead Ends

Common approaches that don't work:

  1. Holding a strong reference to prevent GC 80% fail

    Defeats the purpose of weak references and may cause memory leaks