84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
Why
s = [1, 2, 3, 4, 5, 6]i = 0i = s[i] = 3 The result is: [1, 2, 3, 3, 5, 6] instead of [3, 2, 3, 4, 5, 6]
You can refer to an article I wrote below: Python: The Pitfalls of Chained Assignment
According to Assignment statements:
a = b = c = d = ... = E
is equivalent to
a = E b = E c = E d = E ...
So: i=s[i]=3is equivalent to:
i=s[i]=3
i = 3 s[i] = 3
The assignment in Python is a statement, not an operator, so the expression (a=b) will produce a syntax error, and the assignment statement has no value.
(a=b)
Reference https://stackoverflow.com/que...
s = [1, 2, 3, 4, 5, 6] i = 0 temp_value = 3 i = temp_value s[i] = temp_value
First, i becomes 3, and then the value s[i] is assigned
Looking back at the results, i=3 was executed before s[i] = 3.
Can’t you just write it in two separate sentences?
You can use PythonTutor.com i = s[i] = 3 That line basically executes i=3 and s[i]=3 successively
i = s[i] = 3 is equivalent to i = 3; s[i] = 3
i = s[i] = 3
i = 3; s[i] = 3
Use dis module to analyze the execution process:
>>> def f(): s = [1, 2, 3, 4, 5, 6] i = 0 i = s[i] = 3 >>> import dis >>> dis.dis(f) 2 0 LOAD_CONST 1 (1) 3 LOAD_CONST 2 (2) 6 LOAD_CONST 3 (3) 9 LOAD_CONST 4 (4) 12 LOAD_CONST 5 (5) 15 LOAD_CONST 6 (6) 18 BUILD_LIST 6 21 STORE_FAST 0 (s) # s = [1, 2, 3, 4, 5, 6] 3 24 LOAD_CONST 7 (0) 27 STORE_FAST 1 (i) # i = 0 4 30 LOAD_CONST 3 (3) # 常量3 入栈 33 DUP_TOP # 复制栈顶,也就是 常量3 34 STORE_FAST 1 (i) # i = 3 37 LOAD_FAST 0 (s) 40 LOAD_FAST 1 (i) 43 STORE_SUBSCR # s[i] = 3 44 LOAD_CONST 0 (None) # 返回 None 47 RETURN_VALUE
Example of separate writing
>>> def f2(): s = [1, 2, 3, 4, 5, 6] i = 0 i = 3 s[i] = 3 >>> dis.dis(f2) 2 0 LOAD_CONST 1 (1) 3 LOAD_CONST 2 (2) 6 LOAD_CONST 3 (3) 9 LOAD_CONST 4 (4) 12 LOAD_CONST 5 (5) 15 LOAD_CONST 6 (6) 18 BUILD_LIST 6 21 STORE_FAST 0 (s) # s = [1, 2, 3, 4, 5, 6] 3 24 LOAD_CONST 7 (0) 27 STORE_FAST 1 (i) # i = 0 4 30 LOAD_CONST 3 (3) 33 STORE_FAST 1 (i) # i = 3 5 36 LOAD_CONST 3 (3) 39 LOAD_FAST 0 (s) 42 LOAD_FAST 1 (i) 45 STORE_SUBSCR # s[i] = 3 46 LOAD_CONST 0 (None) 49 RETURN_VALUE >>>
You can refer to an article I wrote below: Python: The Pitfalls of Chained Assignment
According to Assignment statements:
is equivalent to
So:
i=s[i]=3
is equivalent to:The assignment in Python is a statement, not an operator, so the expression
(a=b)
will produce a syntax error, and the assignment statement has no value.Reference https://stackoverflow.com/que...
is equivalent to
First, i becomes 3, and then the value s[i] is assigned
Looking back at the results, i=3 was executed before s[i] = 3.
Can’t you just write it in two separate sentences?
You can use PythonTutor.com
i = s[i] = 3 That line basically executes i=3 and s[i]=3 successively
i = s[i] = 3
is equivalent toi = 3; s[i] = 3
Use dis module to analyze the execution process:
Example of separate writing