python - Get the results of dis.dis() in a string -


i trying compare bytecode of 2 things difflib, dis.dis() prints console. way output in string?

uses stringio redefine std out string-like object (python 2.7 solution)

import sys import stringio import dis  def a():     print "hello world"  stdout = sys.stdout # hold onto stdout handle f = stringio.stringio() sys.stdout = f # assign new stdout  dis.dis(a) # run dis.dis()  sys.stdout = stdout # reattach stdout  print f.getvalue() # print contents 

Comments