如何在Python中创建对象的副本? [英] How can I create a copy of an object in Python?

查看:231
本文介绍了如何在Python中创建对象的副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个对象的副本。我想让新对象拥有旧对象的所有属性(字段的值)。但我想有独立的对象。因此,如果我改变新对象的字段的值,旧的对象不应该受到影响。

I would like to create a copy of an object. I want the new object to possess all properties of the old object (values of the fields). But I want to have independent objects. So, if I change values of the fields of the new object, the old object should not be affected by that.

推荐答案

有请查看 copy.deepcopy() 函数。它应该做你所需要的。另请参阅此答案以查找相关问题一个很好的解释。

Have a look at the copy.deepcopy() function. It should do exactly what you need. Also have a look at this answer to a related question for a nice explanation.

这篇关于如何在Python中创建对象的副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆