gc getreferrers can be used to see objects before they are fully built note that this is only an example there are many ways to crash python by using gc getreferrers as well as many extension modules even when they are using perfectly documented patterns to build objects identifying and removing all places that expose to the gc a partiallybuilt object is a longterm project a patch was proposed on sf specifically for this example but i consider fixing just this single example a bit pointless 1517042 a fix would include a wholescale code review possibly with an api change to decouple object creation and gc registration and according fixes to the documentation for extension module writers it s unlikely to happen though so this is currently classified as gc getreferrers is dangerous use only for debugging now the marker is in the tuple being constructed now the marker is in the tuple being constructed
import gc


def g():
    marker = object()
    yield marker
    
    [tup] = [x for x in gc.get_referrers(marker) if type(x) is tuple]
    print(tup)
    print(tup[1])


tuple(g())