Skip to content

Instantly share code, notes, and snippets.

@coderplay
Created January 1, 2018 18:56
Show Gist options
  • Save coderplay/30a392dae8fbc8d9053670af4893559f to your computer and use it in GitHub Desktop.
Save coderplay/30a392dae8fbc8d9053670af4893559f to your computer and use it in GitHub Desktop.
python bytecode
bash-3.2$ python
Python 2.7.10 (default, Feb 6 2017, 23:53:20)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12):
def func(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12):
... return arg1+arg2+arg3+arg4+arg5+arg6+arg7+arg8+arg9+arg10+arg11+arg12
return arg1+arg2+arg3+arg4+arg5+arg6+arg7+arg8+arg9+arg10+arg11+arg12
...
>>> import dis
import dis
>>> dis.dis(func.func_code)
dis.dis(func.func_code)
2 0 LOAD_FAST 0 (arg1)
3 LOAD_FAST 1 (arg2)
6 BINARY_ADD
7 LOAD_FAST 2 (arg3)
10 BINARY_ADD
11 LOAD_FAST 3 (arg4)
14 BINARY_ADD
15 LOAD_FAST 4 (arg5)
18 BINARY_ADD
19 LOAD_FAST 5 (arg6)
22 BINARY_ADD
23 LOAD_FAST 6 (arg7)
26 BINARY_ADD
27 LOAD_FAST 7 (arg8)
30 BINARY_ADD
31 LOAD_FAST 8 (arg9)
34 BINARY_ADD
35 LOAD_FAST 9 (arg10)
38 BINARY_ADD
39 LOAD_FAST 10 (arg11)
42 BINARY_ADD
43 LOAD_FAST 11 (arg12)
46 BINARY_ADD
47 RETURN_VALUE
>>> def func2(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12):
def func2(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12):
... return (arg1+arg2+arg3+arg4+arg5+arg6+arg7+arg8+arg9+arg10+arg11)*arg12
return (arg1+arg2+arg3+arg4+arg5+arg6+arg7+arg8+arg9+arg10+arg11)*arg12
...
>>> dis.dis(func2.func_code)
dis.dis(func2.func_code)
2 0 LOAD_FAST 0 (arg1)
3 LOAD_FAST 1 (arg2)
6 BINARY_ADD
7 LOAD_FAST 2 (arg3)
10 BINARY_ADD
11 LOAD_FAST 3 (arg4)
14 BINARY_ADD
15 LOAD_FAST 4 (arg5)
18 BINARY_ADD
19 LOAD_FAST 5 (arg6)
22 BINARY_ADD
23 LOAD_FAST 6 (arg7)
26 BINARY_ADD
27 LOAD_FAST 7 (arg8)
30 BINARY_ADD
31 LOAD_FAST 8 (arg9)
34 BINARY_ADD
35 LOAD_FAST 9 (arg10)
38 BINARY_ADD
39 LOAD_FAST 10 (arg11)
42 BINARY_ADD
43 LOAD_FAST 11 (arg12)
46 BINARY_MULTIPLY
47 RETURN_VALUE
>>> def func3(f):
def func3(f):
... return f(1,2)
return f(1,2)
...
>>> dis.dis(func3.func_code)
dis.dis(func3.func_code)
2 0 LOAD_FAST 0 (f)
3 LOAD_CONST 1 (1)
6 LOAD_CONST 2 (2)
9 CALL_FUNCTION 2
12 RETURN_VALUE
>>> dir(dis)
dir(dis)
['EXTENDED_ARG', 'HAVE_ARGUMENT', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_have_code', '_test', 'cmp_op', 'dis', 'disassemble', 'disassemble_string', 'disco', 'distb', 'findlabels', 'findlinestarts', 'hascompare', 'hasconst', 'hasfree', 'hasjabs', 'hasjrel', 'haslocal', 'hasname', 'opmap', 'opname', 'sys', 'types']
>>> def func4(f, arg1, arg2):
def func4(f, arg1, arg2):
... return f(arg1, arg2)
return f(arg1, arg2)
...
>>> dis.dis(func4.func_code)
dis.dis(func4.func_code)
2 0 LOAD_FAST 0 (f)
3 LOAD_FAST 1 (arg1)
6 LOAD_FAST 2 (arg2)
9 CALL_FUNCTION 2
12 RETURN_VALUE
>>> def test_var_args(farg, *args):
print "formal arg:", farg
for arg in args:
print "another arg:", arg
def test_var_args(farg, *args):
print "formal arg:", farg
for arg in args:
print "another arg:", arg
... ... ... ...
>>> dis.dis(test_var_args)
dis.dis(test_var_args)
2 0 LOAD_CONST 1 ('formal arg:')
3 PRINT_ITEM
4 LOAD_FAST 0 (farg)
7 PRINT_ITEM
8 PRINT_NEWLINE
3 9 SETUP_LOOP 23 (to 35)
12 LOAD_FAST 1 (args)
15 GET_ITER
>> 16 FOR_ITER 15 (to 34)
19 STORE_FAST 2 (arg)
4 22 LOAD_CONST 2 ('another arg:')
25 PRINT_ITEM
26 LOAD_FAST 2 (arg)
29 PRINT_ITEM
30 PRINT_NEWLINE
31 JUMP_ABSOLUTE 16
>> 34 POP_BLOCK
>> 35 LOAD_CONST 0 (None)
38 RETURN_VALUE
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment