python + = = the difference between

 1 a = 1
 2 print('a', a, id(a))
 3 b = a
 4 a += 1
 5 print('a', a, id(a))
 6 print('b', b, id(b))
 7 
 8 print('-' * 20)
 9 a = a + 1
10 print('a', a, id(a))
11 print('b', B, ID (B))
 12 is  
13 is  # following output: 
14 A 140,721,411,760,528. 1
 15 A 2 140,721,411,760,560
 16 B 140,721,411,760,528. 1
 . 17 --------------------
 18 is A. 3 140,721,411,760,592
 . 19 B 140,721,411,760,528. 1

As described above, after a + 1 =, a change in the address, and b = a is a pointer to a former address, and when a change released, the source address unchanged, the value of b and the address is not changed

. 1 A = [. 1, 2 ]
 2  Print (ID (A))
 . 3 F = A
 . 4 A + = [. 3,. 4 ]
 . 5  Print (ID (A))
 . 6  Print (F)
 . 7  
. 8  Print ( ' _ ' * 20 is )
 . 9 B = [. 1, 2 ]
 10  Print (ID (B))
 . 11 G = B
 12 is B = B + [. 3,. 4 ]
 13 is  Print (ID (B))
 14  Print (G)
 15  
16  # outputs the following : 
17 1534352052616
18 1534352052616
19 [1, 2, 3, 4]
20 ____________________
21 1534352053128
22 1534352609736
23 [1, 2]

As shown above, we use the list to compare, you will find different from before:

a + = a and the same [3, 4] address, does not change, and the value of f is changed, the value after a change is equal, indicating a source address unchanged, value changes;

and b = b + b different [3, 4] address, indicating address changes, g is pointing to the same address 

Guess you like

Origin www.cnblogs.com/tttzqf/p/12563612.html