为什么上一行代码在下一行之后运行?

时间:2019-07-01 16:59:56

标签: python

我正在解决我的考试卷中提出的一个问题。

three = one[0] ([3:4])  # line2
one[0][0] = 5  ([5:4])  # line3

但是,当我运行代码时,我意识到即使第2行在第3行上方,第2行也变为([5:4])

two[1][1] = 8 ([5:8])  # line5
[line3] -> 'one' becomes [5:8] even though line3 is above line5

我很困惑,为什么上面的代码在运行后会发生变化。

    def perform_magic(one, two):  #line1
    three = one[0]                #line2
    one[0][0] = 5                 #line3
                                  #line4
    two.append(three)             #line5
    two[1][1] = 8                 #line6
    two.append([6,7])             #line7
                                  #line8
one = [[3,4]]                     #line9
two = [[1,2]]                     #line10
                                  #line11
perform_magic(one, two)           #line12
                                  #line13
print(one[0])                     #line14
print(two[2])                     #line15

正确答案:

[5,8]
[6,7]

我期望的是:

[5,4]
[6,7]

0 个答案:

没有答案