Question
# Write your own version of dis.dis() that produces a dictionary object # containing the following values: # # names: a list of the function's
# Write your own version of dis.dis() that produces a dictionary object
# containing the following values: # # names: a list of the function's names (the .co_varnames attribute) # consts: a list of the function's constants (the .co_consts attribute) # code: a list of the function's bytecode (the .co_code attribute) # instructions: a list of the sequence of instructions for the function. # # Each instruction list has the following four components # # adjusted line number: index position in the bytecode list # instruction opcode number: opcode from the bytecode list # opname: symbolic name for the opcode # index: if the opcode takes an argument, the numeric index value, else None # argument: if the opcode takes an argument, the value of the argument, else None
# makeobj(s1) => # {'instructions': # [(0, 100, 'LOAD_CONST', 1, 1), # (3, 125, 'STORE_FAST', 0, 'a'), # (6, 124, 'LOAD_FAST', 0, 'a'), # (9, 83, 'RETURN_VALUE', None, None)], # 'names': ('a',), # 'code': b'd\x01\x00}\x00\x00|\x00\x00S', # 'consts': (None, 1)}
# Note that makeobj is similar to dis.dis(). You may use dis.dis() # to help test your implementation of makeobj() # # You may also want to use dis.HAVE_ARGUMENT to identify those # opcodes that do and do not take an argument
def makeobj(fun): result = {} names = fun.__code__.co_varnames result['names'] = names consts = fun.__code__.co_consts result['consts'] = consts code = fun.__code__.co_code result['code'] = code codelen = len(code) instructions = [] ## fill in the rest here result['instructions'] = instructions return result
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started