I am trying to create a start copy of a variable that I would like to compare at the end with the processed variable. Here is what I currently have:
def sudoku_solver(sudoku):
global start_sudoku
start_sudoku = copy.deepcopy(sudoku)
solution(sudoku)
......
I am trying to define this as a global variable as I have a second def function
I use to process the solution and in the end I would like to compare if the two solutions are the same or not:
def self.solution(sudoku):
......
if (len(sudoku) == len(start_sudoku)) == True:
if possible(y, x, n):
......
def self.possible(y, x, n):
however the two items are always the same when I get to the end of the code. I checked and the ID's are different however they always end up the same. How can the global variable remain independent of the first one and maintain the first view of the information only?
CodePudding user response:
Your logic suggests that you need state, which is what classes are for. Using a class is the cleaner approach than dealing with global variables.
class SudokuSolver:
def __init__(self, sudoku):
self.start_sudoku = copy.deepcopy(sudoku)
self.solution(sudoku)
...
def solution(self, sudoku):
...
if len(sudoku) == len(self.start_sudoku):
if self.possible(y, x, n):
...
def possible(self, y, x, n):
...