Skip to content
Advertisement

Imported function changes when running changes imported variable

I am coding Chess and for past three days i’ve been stuck on this problem: When i run simulate_play_and_check function which is imported from funcs.py file it takes arguments and changes them in a main file and i dont know why. When i print board.board before running this function it prints:

JavaScript

After running the function it prints:

JavaScript

Here is sample code of this: main.py

JavaScript

funcs.py

JavaScript

I am quite new in coding, but i want to know board.board changes after running that function.

Advertisement

Answer

Problem is in that when i assign board to a temp it is still on that same place in memory. So when i change something in that temp variable it changes the spot in the memory where is the variable board. Doing copy like that is called shallow copy if i am not mistaken. And you need to create something called deep copy if you are copying the list you need to create new empty list then iterate through the board variable and copy it to the new list like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement