Skip to content

Instantly share code, notes, and snippets.

@narfdotpl
Created May 2, 2014 20:42
Show Gist options
  • Save narfdotpl/dd70fe715478536ccbbf to your computer and use it in GitHub Desktop.
Save narfdotpl/dd70fe715478536ccbbf to your computer and use it in GitHub Desktop.
Python: += vs +
Python 2.7.2 (default, Oct 17 2011, 00:04:42)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from dis import dis
>>> def short(xs):
... xs[0] += 1
...
>>> dis(short)
2 0 LOAD_FAST 0 (xs)
3 LOAD_CONST 1 (0)
6 DUP_TOPX 2
9 BINARY_SUBSCR
10 LOAD_CONST 2 (1)
13 INPLACE_ADD
14 ROT_THREE
15 STORE_SUBSCR
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
>>> def long(xs):
... xs[0] = xs[0] + 1
...
>>> dis(long)
2 0 LOAD_FAST 0 (xs)
3 LOAD_CONST 1 (0)
6 BINARY_SUBSCR
7 LOAD_CONST 2 (1)
10 BINARY_ADD
11 LOAD_FAST 0 (xs)
14 LOAD_CONST 1 (0)
17 STORE_SUBSCR
18 LOAD_CONST 0 (None)
21 RETURN_VALUE
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment