# Pastebin Rx5HwNWJ class A: def f(self): return 1 def callf(): a = A() # warm up method cache for i in range(100): x = a.f() return x print("before hack", callf()) import gc d, = gc.get_referents(A.__dict__) # get the underlying dict from the mappingproxy d['f'] = lambda self: "evil" print("after hack", callf())