Python: names, values, assignment and mutability
推荐先看视频(youtube) Ned Batchelder - Facts and Myths about Python names and values - PyCon 2015
Change variable
# rebinding
x = x + 1
# mutating
nums.append(7)
# can also rebind lists:
nums = nums + [7]
# but you can't mutate immutable
make a new list, don't change the mutable param
def append_twice(a_list, val):
a_list.append(val)
a_list.append(val) def append_twice_bad(a_list, val):
"""This function is useless"""
a_list = a_list + [val, val] def append_twice_good(a_list, val):
a_list = a_list + [val, val]
return a_list
shadowcopy and deepcopy
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
通过图片了解 compound objects
# objects that contain other objects, like lists or class instances
items = [{'id': 1, 'name': 'laptop', 'value': 1000}, {'id': 2, 'name': 'chair', 'value': 300},]
# 但是含有的是 int 这样的就不属于 compound objects int 疑问:难道不属于 object? items = [1, 2, 3]
看看 shadow copy 与 deepcopy 对 compound object 的不同
from copy import deepcopy, copy items = [{'id': 1, 'name': 'laptop', 'value': 1000}, {'id': 2, 'name': 'chair', 'value': 300},]
items_deep_copy = deepcopy(items)
items_deep_copy[1]['id'] = 'b'
items_deep_copy == items items_copy = copy(items)
items_copy[1]['id'] = 'b'
items_copy == items
可以看出,deep_copy 是全部重新复制,而 shadow copy 只 deep copy复制了第一层,嵌套的只是复制了引用
疑问:
Python 3.6.0 (default, Dec 24 2016, 08:01:42) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
items = [1, 2, 3, 4, 5, 6]
items_copy = items[:]
items_copy[0] = 'a'
items_copy == items
False
# I think this is a shallow copy, and items_copy == items should return True but it's False.
# But another example return True
items = [{'id': 1, 'name': 'laptop', 'value': 1000}, {'id': 2, 'name': 'chair', 'value': 300},]
items_copy = items[:]
items_copy[0]['id'] = 'a'
items_copy == items
True
疑问:Do all mutable objects are composed of immutable objects
[1, 2, 3] from 1, 2, 3
Like language are composed of words
最新文章
- JavaWeb_day06_Filter过滤器
- HTML+CSS代码橙色导航菜单
- HTTP状态码大全
- param STRING $username 要检查的用户名
- 一、Java语言基础
- acdream 1683 村民的怪癖(KMP,经典变形)
- react初识
- 转自http://blog.sina.com.cn/daylive——C++ STL map
- mysql函数二
- 解决Flink输出日志中时间比当前时间晚8个小时的问题
- 条件随机场CRF(三) 模型学习与维特比算法解码
- 探讨SELECT语句的元数据&;amp;动态取样&;amp;读一致性导致的一致性读和递归操作
- Redis 缓存失效和回收机制续
- 相片后期处理,PS调出温暖的逆光美女
- Android 下载App
- Nessus漏洞扫描教程之使用Nmap工具扫描识别指纹
- 【Android】Android六种布局详解
- 大数据智能SOC解决方案
- THINKPHP include 标签动态加载文件
- iOS: 详细的正则表达式
热门文章
- DLL内存分配与共享
- Service(1)
- css - 紧贴底部的页脚
- webpack 通用环境快速搭建
- 打包Cocos2d-xproject为PC项目
- Triangulation by Ear Clipping(耳切法处理多边形三角划分)
- MyEclipse 2013安装后要做的几件事
- Python常见经典 python中if __name__ == '__main__': 的解析
- sqrt函数实现(神奇的算法)
- hdu5673 Robot 卡特兰数+组合数学+线性筛逆元