问题描述
我想检查我是否正确理解了内存分配,起初我以为我是在使用例如 b[0,:,0].copy() (如测试 2 中)来防止 Python 中的可变性,你能请确认我是否正确理解了这些概念?
e
PrototypeBean proto = beanfactory.getBean(PrototypeBean.class);
结果
import numpy as np
import copy
print("Base")
a = np.zeros(shape=(2,2,2))
c = np.ones(shape=(2,2))
b = c
a[0,0] = b[0,0] + c[0,0]
c[0,0] = c[0,0] * 100
print("After (b is mutated))")
print("a",a)
print("b",b)
print("c",c)
print("Test 1 using copy()")
a = np.zeros(shape=(2,0].copy() + c[0,0] * 100
print("After (b is still mutated))")
print("a",c)
print("Test 2 properly using copy()")
a = np.zeros(shape=(2,2))
b = c.copy()
a[0,0] * 100
print("After (b is not mutated))")
print("a",c)
很抱歉这个非常基本的问题,但我想确保我正确理解。 非常感谢提前
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)