简体   繁体   English

在多个文件中使用但在函数调用后未更改的全局变量(Python)

[英]Global variable used in multiple files but not changed after function call (Python)

I declared a global variable in my settings.py module:我在 settings.py 模块中声明了一个全局变量:

def init():
    global grid
    grid = [[0,0,0,0,0,0,1,4,3],
            [9,0,0,0,0,0,0,0,0],
            [0,0,2,0,4,5,0,8,0],
            [0,0,0,0,0,3,0,1,0],
            [0,6,0,4,9,1,0,5,0],
            [0,4,0,6,0,0,0,0,0],
            [0,8,0,7,5,0,4,0,0],
            [0,0,0,0,0,0,0,0,8],
            [2,1,5,0,0,0,0,0,0]]

I have an other module called run.py, that contains a function that should modify grid:我还有一个名为 run.py 的模块,它包含一个应该修改网格的函数:

import numpy
import settings

def solve():

    for x in range(9):
        for y in range(9):
            if settings.grid[x][y] == 0:
                for number in range(1,10):
                    if possible(x,y,number): #function described in this file, not usefull here
                        settings.grid[x][y] = number
                        solve()
                        settings.grid[x][y] = 0
                return

    print("Solved sudoku in solve():\n{}\n".format(numpy.matrix(settings.grid))) #2nd output

In my main.py, I first initialize settings.grid by calling settings.init().在我的 main.py 中,我首先通过调用 settings.init() 来初始化 settings.grid。 After, I basically print the grid variable from settings.py, then call run.solve() to modify it (it is working since the 2nd output will replace the 0s) and then reprint settings.grid to see if the variable was changed accordingly to the 2nd output:之后,我基本上从 settings.py 打印网格变量,然后调用 run.solve() 对其进行修改(它正在工作,因为第二个输出将替换 0),然后重新打印 settings.grid 以查看变量是否相应更改到第二个输出:

import numpy
import settings
import run


settings.init()

print("Inserted sudoku:\n{}\n".format(numpy.matrix(settings.grid)))

run.solve()

print("Solved sudoku:\n{}\n".format(numpy.matrix(settings.grid)))

My problem is, the 3rd output is not taking the same value of settings.grid like the 2nd one like if run.solve() was not even called!我的问题是,第三个输出的 settings.grid 值与第二个不同,就像甚至没有调用 run.solve() 一样!

This is the ouputs i get:这是我得到的输出:

1st output 
Inserted sudoku:
[[0 0 0 0 0 0 1 4 3]
 [9 0 0 0 0 0 0 0 0]
 [0 0 2 0 4 5 0 8 0]
 [0 0 0 0 0 3 0 1 0]
 [0 6 0 4 9 1 0 5 0]
 [0 4 0 6 0 0 0 0 0]
 [0 8 0 7 5 0 4 0 0]
 [0 0 0 0 0 0 0 0 8]
 [2 1 5 0 0 0 0 0 0]]

2nd output
Solved sudoku in solve():
[[6 5 8 9 2 7 1 4 3]
 [9 3 4 1 6 8 2 7 5]
 [1 7 2 3 4 5 9 8 6]
 [8 2 9 5 7 3 6 1 4]
 [7 6 3 4 9 1 8 5 2]
 [5 4 1 6 8 2 3 9 7]
 [3 8 6 7 5 9 4 2 1]
 [4 9 7 2 1 6 5 3 8]
 [2 1 5 8 3 4 7 6 9]]

3rd output
Solved sudoku:
[[0 0 0 0 0 0 1 4 3]
 [9 0 0 0 0 0 0 0 0]
 [0 0 2 0 4 5 0 8 0]
 [0 0 0 0 0 3 0 1 0]
 [0 6 0 4 9 1 0 5 0]
 [0 4 0 6 0 0 0 0 0]
 [0 8 0 7 5 0 4 0 0]
 [0 0 0 0 0 0 0 0 8]
 [2 1 5 0 0 0 0 0 0]]

Do you have any idea of how I can get the 3rd output like the 2nd one?你知道我如何获得像第二个一样的第三个输出吗? In other words, do you know how I can change settings.grid value to be changed in my main.py after a run.solve() call?换句话说,您知道如何在 run.solve() 调用后更改要在 main.py 中更改的 settings.grid 值吗?

Thanks in advance提前致谢

I reproduced as possible your situation and I think I get the desired result.我尽可能地复制了你的情况,我想我得到了想要的结果。 This is what I did这就是我所做的

settings.py设置.py

def init():
    global grid
    grid = [0, 0]

Updated to have some recursion更新为有一些递归

run.py运行文件

import settings

def solve(i = 1):
    if i >= 0:
        settings.grid[i] = i + 1
        solve(i - 1)

main.py主文件

import settings
import run

settings.init()

print(settings.grid)

run.solve()

print(settings.grid)

And the new output is:新的输出是:

[0, 0]
[1, 2]
>>> 

Note : In your code after the recursive call you're setting the position back to 0 , it may not be your issue but I think you have to check that out.注意:在递归调用后的代码中,您将位置设置回0 ,这可能不是您的问题,但我认为您必须检查一下。

settings.grid[x][y] = number
solve()
settings.grid[x][y] = 0 # <==== Right here

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

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