# Pastebin 11FrLMyJ import sys import gc import weakref class A: pass def other(a): v = [] # using a list rather than nonlocal for pypy2 support # delete the only remaining ref to a and replace with a weakref a = weakref.ref(a, lambda _: v.append(True)) while not v: # on cpython this branch is never hit, but on pypy we need to sit and # collect, until the object is deleted gc.collect() def main(): # move our reference to the instance of A into other other(*(A(), )) if __name__ == "__main__": sys.exit(main())