Xan*_*unn 0 python side-effects
def group_move(group, damper):
# Make a copy to test values
new = group
# See what the original group value is
print("Test = " + str(group.ctris[0].p1.x))
dr = some float
dx = some float
dy = some float
# Make changes to new
moveGroup(new, dr, dx, dy)
# See if those changes produce allowed values
if (off_board_check(new) == 1):
damper += 2.0
# Reset to original to try again
print("Test Here = " + str(group.ctris[0].p1.x))
group_move(group, damper)
else:
# If everything is on the board, then make the change
group = new
Run Code Online (Sandbox Code Playgroud)
如果我运行它,我将看到在第一次递归时,Test打印行产生与打印行不同的值Test Here.为什么?这段代码如何影响值group?我试图在测试值失败的情况group下将未更改的传递到下一个递归级别group_move,但group在我甚至进行任何递归调用之前,似乎在某种程度上受到影响.以上与此有何不同:
>>> x = 1
>>> y = x
>>> x = 7
>>> y = 77
>>> x
7
>>> y
77
Run Code Online (Sandbox Code Playgroud)
# Make a copy to test values
new = group
Run Code Online (Sandbox Code Playgroud)
评论不正确.那不是副本.所做的就是在new指向的同一个对象上创建一个名为point 的变量group.
如果要创建实际副本,可能需要查看copy.deepcopy().