6

I was going through the dis package of python. I tried code to see how it is working

>>> def get():
...     x=4
...     y=x+3 ............ this line
...     z=8
...     return y

and then run dis.dis(get) which prints the byte-code instruction for above code.

for x+3 byte-code instruction is BINARY_ADD and when went trough package docs, I found another term INPLACE_ADD

Although in there they have mentioned what is INPLACE_ADD but I did not get the difference.

I have two question

a). what is difference between BINARY_ADD and INPLACE_ADD

b). how can I write code so that byte-code instruction shows INPLACE_ADD

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
navyad
  • 3,752
  • 7
  • 47
  • 88
  • 2
    `+=` is called in-place add. possible duplicate of [When is "i += x" different from "i = i + x" in Python?](http://stackoverflow.com/questions/15376509/when-is-i-x-different-from-i-i-x-in-python) – Ashwini Chaudhary Sep 19 '13 at 10:56

1 Answers1

6

INPLACE_ADD is the += operator:

>>> from dis import dis
>>> def f():
...     x += y
...     
>>> dis(f)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_GLOBAL              0 (y)
              6 INPLACE_ADD         
              7 STORE_FAST               0 (x)
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
Fred Foo
  • 355,277
  • 75
  • 744
  • 836