modifying python bytecode
问题 I was wondering how to modify byte code, then recompile that code so I can use it in python as a function? I've been trying: a = """ def fact(): a = 8 a = 0 """ c = compile(a, '<string>', 'exec') w = c.co_consts[0].co_code dis(w) which decompiles to: 0 LOAD_CONST 1 (1) 3 STORE_FAST 1 (1) 6 LOAD_CONST 2 (2) 9 STORE_FAST 1 (1) 12 LOAD_CONST 0 (0) 15 RETURN_VALUE supposing I want to get rid of lines 0 and 3, I call: x = c.co_consts[0].co_code[6:16] dis(x) which results in : 0 LOAD_CONST 2 (2) 3