#! /usr/bin/python3.4 import dis import operator import opcode # from display import * ## define a simple function def s(): a = 1 b = 2 return (a + b) ## check out the attributes of the function dir(s) ## check out the attributes of the function's __code__ attribute dir(s.__code__) ## print out the value of the attributes of the __code__ attribute def xf(func, all=False): for x in (dir(func.__code__)): if all: print ("{}:\t{}".format(x, func.__code__.__getattribute__(x))) elif x.startswith("co"): print ("{}:\t{}".format(x, func.__code__.__getattribute__(x))) ## retrieve the bytecodes def getbytes(f): return f.__code__.co_code ## print out the bytecodes def printbytes(f): for b in getbytes(f): print (b) ## the opcode module can help tell us what these bytecodes mean. dir(opcode) ## print out the opcodes def printopcodes(f): for b in getbytes(f): print ("{}: {}".format(b, opcode.opname[b])) ## the dis module can really disassemble the bytecodes def showme(f): dis.dis(f) print(dis.code_info(f))