import os, inspect source_org = """ def foo(): pass """ source_new = """ def foo(): return 1 """ somefile = os.tmpnam() file(somefile, 'w').write(source_org) execfile(somefile) print inspect.findsource(foo) file(somefile, 'w').write(source_new) print inspect.findsource(foo)