简体   繁体   English

变量 scope 是错误的全局变量

[英]Variable scope is incorrectly global

My original variable seems to be global since it is changed after passing a function, updateSudoku .我的original变量似乎是全局的,因为它在通过 function, updateSudoku From my understanding, the variable scope inside a function should be local to that function only.据我了解,function 中的变量 scope 应该仅是 function 的本地变量。 Could someone explain why code is failing?有人可以解释为什么代码失败了吗?

For example, the value in the lower left corner has correctly been changed but a test on my variable show that they are equal.例如,左下角的值已正确更改,但对我的变量的测试表明它们是相等的。 Somehow, along the way, I changed the variable original without referring to it.不知何故,在此过程中,我更改了变量original而不参考它。

From the python documentation, I found the following but I am not sure how it helps me out.从 python 文档中,我发现了以下内容,但我不确定它如何帮助我。 In Python, variables that are only referenced inside a function are implicitly global.在 Python 中,仅在 function 内部引用的变量是隐式全局的。 If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global.如果一个变量在函数体内的任何地方都被赋值,除非明确声明为全局变量,否则它被假定为局部变量。

Code:代码:

def print_sudoku(board):
    print("-"*37)
    for i, row in enumerate(board):
        print(("|" + " {}   {}   {} |"*3).format(*[x if x != 0 else " " for x in row]))
        if i == 8:
            print("-"*37)
        elif i % 3 == 2:
            print("|" + "---+"*8 + "---|")
        else:
            print("|" + "   +"*8 + "   |")

original = [[0, 0, 1, 0, 3, 0, 0, 0, 0], [0, 3, 0, 0, 0, 6, 1, 7, 0], [0, 0, 0, 0, 0, 0, 0, 3, 0], [0, 6, 7, 3, 1, 0, 8, 0, 9], [8, 1, 3, 4, 0, 0, 5, 0, 7], [0, 2, 0, 0, 8, 7, 0, 1, 3], [3, 0, 0, 0, 0, 0, 7, 0, 0], [7, 0, 0, 0, 0, 0, 3, 5, 0], [0, 0, 6, 0, 0, 3, 0, 0, 0]]

updates = [[], [], [(1, [(8, 0)])], [(1, [(2, 5)])], [], [], [], [], []]

print_sudoku(original)

def updateSudoku(sudoku, updates):

    newVar=sudoku

    for b in updates:
        for soln in b:
            newVar[soln[1][0][0]][soln[1][0][1]]=soln[0] 


    return newVar

newSudoku=updateSudoku(original, updates)

print_sudoku(newSudoku)
print(original==newSudoku)

Output: Output:

-------------------------------------
|         1 |     3     |           |
|   +   +   +   +   +   +   +   +   |
|     3     |         6 | 1   7     |
|   +   +   +   +   +   +   +   +   |
|           |           |     3     |
|---+---+---+---+---+---+---+---+---|
|     6   7 | 3   1     | 8       9 |
|   +   +   +   +   +   +   +   +   |
| 8   1   3 | 4         | 5       7 |
|   +   +   +   +   +   +   +   +   |
|     2     |     8   7 |     1   3 |
|---+---+---+---+---+---+---+---+---|
| 3         |           | 7         |
|   +   +   +   +   +   +   +   +   |
| 7         |           | 3   5     |
|   +   +   +   +   +   +   +   +   |
|         6 |         3 |           |
-------------------------------------
-------------------------------------
|         1 |     3     |           |
|   +   +   +   +   +   +   +   +   |
|     3     |         6 | 1   7     |
|   +   +   +   +   +   +   +   +   |
|           |         1 |     3     |
|---+---+---+---+---+---+---+---+---|
|     6   7 | 3   1     | 8       9 |
|   +   +   +   +   +   +   +   +   |
| 8   1   3 | 4         | 5       7 |
|   +   +   +   +   +   +   +   +   |
|     2     |     8   7 |     1   3 |
|---+---+---+---+---+---+---+---+---|
| 3         |           | 7         |
|   +   +   +   +   +   +   +   +   |
| 7         |           | 3   5     |
|   +   +   +   +   +   +   +   +   |
| 1       6 |         3 |           |
-------------------------------------
True

Global scope is irrelevant in this case.在这种情况下,全局 scope 无关紧要。

What is going on here is that you have passed original , which is a list, as a parameter to the updateSudoku() function.这里发生的事情是您已将original (一个列表)作为参数传递给updateSudoku() function。 Lists are mutable objects, therefore any modification you make to the list within that function will modify the list in place.列表是可变对象,因此您对该 function 中的列表所做的任何修改都将修改该列表。 It will change the original list's contents.它将更改original列表的内容。

If you want to avoid this, you should deep copy the list within the function.如果你想避免这种情况,你应该深拷贝 function 中的列表。

I recommend:我建议:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM