Skip to content

Instantly share code, notes, and snippets.

@brannerchinese
Created December 5, 2013 19:43
Show Gist options
  • Save brannerchinese/7812512 to your computer and use it in GitHub Desktop.
Save brannerchinese/7812512 to your computer and use it in GitHub Desktop.
compare two methods of initializing zeroed list, Python 3.3
In [1]: import dis
In [2]: def f(): return ["foo" for i in range(10)]
In [3]: dis.dis(f)
1 0 LOAD_CONST 1 (<code object <listcomp> at 0x1062e14b0, file "<ipython-input-16-3cb1faa7b4d7>", line 1>)
3 LOAD_CONST 2 ('f.<locals>.<listcomp>')
6 MAKE_FUNCTION 0
9 LOAD_GLOBAL 0 (range)
12 LOAD_CONST 3 (10)
15 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
18 GET_ITER
19 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
22 RETURN_VALUE
In [4]: def f(): return ["foo"] * 10
In [5]: dis.dis(f)
1 0 LOAD_CONST 1 ('foo')
3 BUILD_LIST 1
6 LOAD_CONST 2 (10)
9 BINARY_MULTIPLY
10 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment