Home  >  Q&A  >  body text

Python newbie asked about the difference between a+=a and a=a+a

First code:

# -*- coding:gb2312 -*-

a = [100]

def test(num):
    num += num #第一段代码
    print(num)
    
test(a)
print(a)

Results of the:

Second code:

# -*- coding:gb2312 -*-

a = [100]

def test(num):
    num = num + num  #这个地方改了一下
    print(num)
    
test(a)
print(a)

Results of the:

My question:
Shouldn't num = num be directly equivalent to mun = num num?
Why are the calculated results different? What is going on

三叔三叔2685 days ago1130

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-06-13 09:26:36

    You can try to do something like this,

    In [1]: a = [100]
    
    In [2]: b = [100]
    
    In [3]: id(a)
    Out[3]: 79308552L
    
    In [4]: id(b)
    Out[4]: 79342728L
    
    In [5]: a += a
    
    In [6]: b = b + b
    
    In [7]: id(a)
    Out[7]: 79308552L
    
    In [8]: id(b)
    Out[8]: 79341192L

    The memory address allocated to the variable can be obtained through the id() function. Through experiments, it was found that the variable address using + has changed, which is what you said num+=num and num=num+numare not equivalent.
    However, when you do the following sexy operations, you will find yourself slapped in the face

    In [19]: a = (0,)
    
    In [20]: b = (0,)
    
    In [21]: id(a)
    Out[21]: 82230688L
    
    In [22]: id(b)
    Out[22]: 82208920L
    
    In [23]: a += a
    
    In [24]: b = b + b
    
    In [25]: id(a)
    Out[25]: 79268296L
    
    In [26]: id(b)
    Out[26]: 79328392L

    The assigned address seems to keep changing.
    The reason is that data structures in Python are divided into mutable and immutable.
    For variable types, = and += are obviously different, as shown in the list above:
    + represents a connection operation, += represents appending
    For immutable types, = and += are the same operations, such as the tuple above
    The essence of variable types and immutable types lies in whether the memory space is variable~

    reply
    0
  • PHP中文网

    PHP中文网2017-06-13 09:26:36

    The first thing to notice is the difference

    In [26]: def test(num):
        ...:     num = num + num
        ...:     print (num)
        ...:
    
    In [27]: def test1(num):
        ...:     num += num
        ...:     print (num)
        ...:
    
    In [28]: import dis
    
    In [29]: dis.dis(test)
      2           0 LOAD_FAST                0 (num)
                  3 LOAD_FAST                0 (num)
                  6 BINARY_ADD  #区别在这儿
                  7 STORE_FAST               0 (num)
    
      3          10 LOAD_FAST                0 (num)
                 13 PRINT_ITEM
                 14 PRINT_NEWLINE
                 15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE
    
    In [30]: dis.dis(test1)
      2           0 LOAD_FAST                0 (num)
                  3 LOAD_FAST                0 (num)
                  6 INPLACE_ADD  #看这儿
                  7 STORE_FAST               0 (num)
    
      3          10 LOAD_FAST                0 (num)
                 13 PRINT_ITEM
                 14 PRINT_NEWLINE
                 15 LOAD_CONST               0 (None)
                 18 RETURN_VALUE
    

    You can see that the methods called are different, they are __add__, __iadd__
    The addition operator will calculate a new object to assign to num
    The incremental assignment operator modifies the original reference

    Reference here: https://stackoverflow.com/que...

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-13 09:26:36

    Remember that arguments are passed by assignment in Python.

    In Python, assignment is used to pass parameters, not reference, so when you pass a to a function, you pass the value of a, not a itself. If you want to change a itself, you need to use return to pass the value back

    a = [100]
    
    def test(num):
        num = num + num  #这个地方改了一下
        return(num)      #这个地方再改了一下
        
    print(test(a))       #傳值回來
    print(a)
    a = test(a)
    print(a)
    

    Result:

    [100, 100]
    [100]
    [100, 100]
    

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-13 09:26:36

    In python, a=a+b means first creating a new object and letting variable a refer to this object. a+=b changes the value of the object referenced by a into the value of a+b

    reply
    0
  • Cancelreply