如何知道对象何时被深度复制 Python
How to know when an object is deep copied in Python
我刚刚偶然发现了一个我们都讨厌的问题:当代码有效但你不知道为什么。
有两个class。一个框架和一堆框架。首先,我创建了一个 class Frame 的对象,我称之为临时框架,并向其中添加了一个项目。然后,我将此帧推送到 class 堆栈的对象 lf_stack。然后,我通过创建一个新的 class 来覆盖变量 tf。问题是:为什么这个变化没有反映在堆栈中? python是否将对象的地址压入栈中,意思是当我将一个新对象赋值给变量“tf”时,它会创建一个具有新地址的新对象,而栈中的对象保持不变?
编辑:这种行为对我有益,但我需要了解它发生的原因
这是最简单的例子:
class Stack:
def __init__(self):
self.content=[]
def push(self,item):
self.content.append(item)
def pop(self):
self.content.pop()
class Frame:
def __init__(self):
self.variables={}
def add_variable(self,name,content):
self.variables[name]=content
def execute_instruction1():
global lf_stack
lf_stack.push(tf)
def execute_instruction2():
global tf
tf=Frame()
lf_stack.push(tf)
def main():
global lf_stack, tf
lf_stack = Stack()
tf = Frame()
tf.add_variable("Foo",4)
#Here, the temporary frame has a variable Foo with value 4
print(tf.variables)
#Here, after adding the temporary frame to a stack, it remains the same
execute_instruction1()
print(tf.variables)
#Here, however, I overwrite the variable "tf" and it changes, but
#I thought it would also change in the stack, but it remains the same
execute_instruction2()
print(tf.variables)
print(lf_stack.content[0].variables)
if __name__ == '__main__':
main()
每次您的代码执行 tf = Frame()
时,python 首先创建一个新的 Frame
对象,然后使用 tf
引用它以便您可以跟踪它.
当您的代码执行时:lf_stack.push(tf)
,现在 tf
和 lf_stack
的内部都在跟踪相同的 Frame
对象。对象永远不知道跟踪它们的变量的名称(只是变量的数量,因此当此计数变为 0 时它们可以自行销毁)。
但是,下次执行时:tf = Frame()
,和之前一样得到一个新的Frame
对象,但是现在tf
被重新用来跟踪新的[=11] =] 对象。如果 lf_stack
的内部正在跟踪前一个对象,则两者都不会丢失。
另请注意,此处没有发生深拷贝。
我刚刚偶然发现了一个我们都讨厌的问题:当代码有效但你不知道为什么。
有两个class。一个框架和一堆框架。首先,我创建了一个 class Frame 的对象,我称之为临时框架,并向其中添加了一个项目。然后,我将此帧推送到 class 堆栈的对象 lf_stack。然后,我通过创建一个新的 class 来覆盖变量 tf。问题是:为什么这个变化没有反映在堆栈中? python是否将对象的地址压入栈中,意思是当我将一个新对象赋值给变量“tf”时,它会创建一个具有新地址的新对象,而栈中的对象保持不变?
编辑:这种行为对我有益,但我需要了解它发生的原因
这是最简单的例子:
class Stack:
def __init__(self):
self.content=[]
def push(self,item):
self.content.append(item)
def pop(self):
self.content.pop()
class Frame:
def __init__(self):
self.variables={}
def add_variable(self,name,content):
self.variables[name]=content
def execute_instruction1():
global lf_stack
lf_stack.push(tf)
def execute_instruction2():
global tf
tf=Frame()
lf_stack.push(tf)
def main():
global lf_stack, tf
lf_stack = Stack()
tf = Frame()
tf.add_variable("Foo",4)
#Here, the temporary frame has a variable Foo with value 4
print(tf.variables)
#Here, after adding the temporary frame to a stack, it remains the same
execute_instruction1()
print(tf.variables)
#Here, however, I overwrite the variable "tf" and it changes, but
#I thought it would also change in the stack, but it remains the same
execute_instruction2()
print(tf.variables)
print(lf_stack.content[0].variables)
if __name__ == '__main__':
main()
每次您的代码执行 tf = Frame()
时,python 首先创建一个新的 Frame
对象,然后使用 tf
引用它以便您可以跟踪它.
当您的代码执行时:lf_stack.push(tf)
,现在 tf
和 lf_stack
的内部都在跟踪相同的 Frame
对象。对象永远不知道跟踪它们的变量的名称(只是变量的数量,因此当此计数变为 0 时它们可以自行销毁)。
但是,下次执行时:tf = Frame()
,和之前一样得到一个新的Frame
对象,但是现在tf
被重新用来跟踪新的[=11] =] 对象。如果 lf_stack
的内部正在跟踪前一个对象,则两者都不会丢失。
另请注意,此处没有发生深拷贝。