--- title: '' disqus: hackmd --- Instruction Changes === --- Instruction 1: LOAD_DEREF -> LOAD_GLOBAL --- ```gherkin= def a (): pairs = [a, b] def b(): a, b = pairs.pop() ``` > In the example above, when function b is extracted, LOAD_DEREF (pairs) is changed to LOAD_GLOBAL (pairs) --- Instruction 2: LOAD_CONST -> LOAD_FAST (False results!) --- ```gherkin= class P1(Protocol, Generic[T]): def bar(self, x: T) -> str: ... class P2(Generic[T], Protocol): def bar(self, x: T) -> str: ... ``` > Multiple f in bytecode ```gherkin= # Method Name: bar # Filename: innerdata/669.py # Argument count: 2 # Position-only argument count: 0 # Keyword-only arguments: 0 # Number of locals: 2 # Stack size: 1 # Flags: 0x00000053 (NOFREE | NESTED | NEWLOCALS | OPTIMIZED) # First Line: 1143 # Constants: # 0: None # Varnames: # self, x # Positional arguments: # self, x 1143: 0 LOAD_CONST (None) 2 RETURN_VALUE # Method Name: bar # Filename: innerdata/669.py # Argument count: 2 # Position-only argument count: 0 # Keyword-only arguments: 0 # Number of locals: 2 # Stack size: 1 # Flags: 0x00000053 (NOFREE | NESTED | NEWLOCALS | OPTIMIZED) # First Line: 1152 # Constants: # 0: None # Varnames: # self, x # Positional arguments: # self, x 1153: 0 LOAD_FAST (x) 2 RETURN_VALUE ``` --- Instruction 3: LOAD_FAST -> LOAD_CONST --- ```gherkin= class a(x): def f1(self): class b(y): def f2(self, z): pass ``` > When f2 is extracted, the first instruction changes from LOAD_FAST to LOAD_CONST --- TODO: --- >1. Deal with cases like Instruction 2 to see if there are any other changes in bytecode >2. Change order of functions --- Lambda Functions Example 1 --- >This is a section deascribing how lambda functions are converted to bytecode ```gherkin= lambda x: x ``` ```gherkin= # Constants: # 0: <code object <lambda> at 0x7f0f52ab2d40, file "lambda.py", line 6> # 1: '<lambda>' # 2: None 6: 0 LOAD_CONST (<code object <lambda> at 0x7f0f52ab2d40, file "lambda.py", line 6>) 2 LOAD_CONST ('<lambda>') 4 MAKE_FUNCTION (Neither defaults, keyword-only args, annotations, nor closures) 6 POP_TOP 8 LOAD_CONST (None) 10 RETURN_VALUE # Positional arguments: # x 6: 0 LOAD_FAST (x) 2 RETURN_VALUE ``` ```gherkin= def func(x): return x ``` ```gherkin= # Constants: # 0: <code object func at 0x7fe289b43d40, file "lambda.py", line 3> # 1: 'func' # 2: None # Names: # 0: func 3: 0 LOAD_CONST (<code object func at 0x7fe289b43d40, file "lambda.py", line 3>) 2 LOAD_CONST ('func') 4 MAKE_FUNCTION (Neither defaults, keyword-only args, annotations, nor closures) 6 STORE_NAME (func) 8 LOAD_CONST (None) 10 RETURN_VALUE # Positional arguments: # x 4: 0 LOAD_FAST (x) 2 RETURN_VALUE ``` > As seen above, all instructions are the same, except that POP_TOP in lambda has been changed to STORE_NAME. Also, while functions are "func", lambda functions are tagged <lambda> (1: in constants) --- Lambda Functions Example 2 --- ```gherkin= add = lambda x, y: x + y ``` ```gherkin= 14: 0 LOAD_CONST (<code object <lambda> at 0x7effef43ed40, file "lambda.py", line 14>) 2 LOAD_CONST ('<lambda>') 4 MAKE_FUNCTION (Neither defaults, keyword-only args, annotations, nor closures) 6 STORE_NAME (add) 8 LOAD_CONST (None) 10 RETURN_VALUE ``` ```gherkin= def add(x, y): return x + y ``` ```gherkin= 11: 0 LOAD_CONST (<code object add at 0x7f6e286cad40, file "lambda.py", line 11>) 2 LOAD_CONST ('add') 4 MAKE_FUNCTION (Neither defaults, keyword-only args, annotations, nor closures) 6 STORE_NAME (add) 8 LOAD_CONST (None) 10 RETURN_VALUE ```