python =赋值、copy、deep copy
来源:互联网 发布:买钢琴 知乎 编辑:程序博客网 时间:2024/06/18 06:41
import copya = [1,2,3,4]b = a # 赋值时, 把列表的引用传给bprint(id(a))print(id(b))print(a)print(b)b = copy.copy(a) # 对于可变类型变量, 浅拷贝会将引用和内容全部拷贝一份传给b,即递归所有 a.append(5)print(id(a))print(id(b))print(a)print(b)a = (1,2,3,4) b = aprint(id(a))print(id(b))print(a)print(b)b = copy.copy(a) # 对于不可变类型变量, 浅拷贝只将引用传给新变量print(id(a))print(id(b))print(a)print(b)
运行结果:
140718278173768140718278173768[1, 2, 3, 4][1, 2, 3, 4]140718278173768140718278192264[1, 2, 3, 4, 5][1, 2, 3, 4]140718302072248140718302072248(1, 2, 3, 4)(1, 2, 3, 4)140718302072248140718302072248(1, 2, 3, 4)(1, 2, 3, 4)
阅读全文
0 0
- python =赋值、copy、deep copy
- Deep copy & Shallow copy
- Shallow copy & Deep copy
- deep copy
- difference between Python shallow copy and deep copy
- Python: Shallow and deep copy operations
- [Python]Shallow and Deep copy operation
- Deep copy and shadow copy
- Deep Copy and Shallow Copy
- deep copy vs shallow copy
- Deep Copy VS Shallow Copy
- Shallow Copy and Deep Copy
- shallow copy and deep copy
- Shallow Copy与Deep Copy
- shallow copy与deep copy
- Deep copy and shallow copy
- deep copy and shallow copy
- Python---copy()、deepcopy()与赋值的区别
- 24. Swap Nodes in Pairs
- SQL Developer新建连接问题解决
- LaTex(PART X)数学公式的6种定义形式
- 数据结构学习笔记(13)---哈希查找
- 机器学习--线性回归(原理与例子)
- python =赋值、copy、deep copy
- 前端跳槽面试必备
- python通配符之glob模块
- Xcode的编译/运行结果保存的路径
- Druid大数据实时处理的开源分布式系统——介绍
- JPA+Hibernate+Maven环境搭建
- 常用SQL命令
- JavaScript使ifram跨域相互访问及与PHP通信的实例
- 组合逻辑电路和时序逻辑电路比较