当前位置:   article > 正文

python怎样给对象赋值_Python对象赋值、浅拷贝和深拷贝

python 对象赋值

总结:

1、copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象

2、copy.deepcopy 深拷贝 拷贝对象及其子对象

1、对象赋值

will = ["Will", 28, ["Python", "C#", "JavaScript"]]

wilber = will #对象赋值(对象引用)

print id(will)

print will

print [id(ele) for ele in will]

print id(wilber)

print wilber

print [id(ele) for ele in wilber]

will[0] = "Wilber"

will[2].append("CSS")

print id(will)

print will

print [id(ele) for ele in will]

print id(wilber)

print wilber

print [id(ele) for ele in wilber]

结果:

0818b9ca8b590ca3270a3433284dd417.png

下面来分析一下这段代码:

(1)首先,创建了一个名为will的变量,这个变量指向一个list对象,从第一张图中

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/71434
推荐阅读
相关标签
  

闽ICP备14008679号