Jun-17-2020, 05:41 AM
Let me try a different way.
Line 37 creates a new square, and line 39 puts a link to that square into x.
Line 41 is the interesting bit. rotate_clockwise_90 is handed the square and it changes it. This change affects the reference to it in x. So x changes.
Line 43 then puts another link to the square in x. But this is the same link so all the elements in x are identical.
I think a good approach here is to make your functions not modify the data that is passed in That's easily done by making a copy early in the function. This approach is less memory efficient and would be a drawback for larger and more complex manipulations. But it does make them feel more like functions, where you don't expect the input to be modified.
For instance consider changing
Line 37 creates a new square, and line 39 puts a link to that square into x.
Line 41 is the interesting bit. rotate_clockwise_90 is handed the square and it changes it. This change affects the reference to it in x. So x changes.
Line 43 then puts another link to the square in x. But this is the same link so all the elements in x are identical.
I think a good approach here is to make your functions not modify the data that is passed in That's easily done by making a copy early in the function. This approach is less memory efficient and would be a drawback for larger and more complex manipulations. But it does make them feel more like functions, where you don't expect the input to be modified.
For instance consider changing
def rotate_Clockwise_90(B): n1=len(B[0]) ...to
import copy [...] def rotate_Clockwise_90(B): B = copy.deepcopy(B) n1=len(B[0]) ...It's not the only way to go. You could instead make the copies in the main logic, allowing the functions to continue modifying the data. Either way is a possible solution.