def func(s): return len(s) text = "abc" print(func(text)) FuncType = type(func) func_globals = {} code = func.__code__ func2 = FuncType(code, func_globals) print(func2(text))