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
Python: names, values, assignment and mutability的更多相关文章
- Python 字典 values() 方法
描述 Python 字典 values() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有值. 语法 values() 方法语法: D.values() 参数 无 ...
- Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.
近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况. 报错信息如下: test_room.py:None (test_room.py) ImportErr ...
- pytest框架执行自动化测试时使用pycharm正常运行,使用cmd或Terminal报错:Hint: make sure your test modules/packages have valid Python names.
问题描述: 使用pytest框架做接口自动化测试时,在测试用例所在的.py文件下使用pycharm的run功能可以正常跑用例,使用cmd运行窗口或Terminal则报下图中的错误: Hint: mak ...
- 短篇文档兼职看过来 python 课后作业 assignment project
文档兼职 开题报告 读后感 课后作业 等 代写 编程,Java ,Python,R,等语言的,国内外课程作业指导,写作. 有经验,有作品,成交快,放心! 可联系 QQ 550987425
- python two-dimensional array assignment initialize
#if you want to initialize a 9*9 two-dimensional array [([""]*9) for i in range(9)] #cauti ...
- Python中的passed by assignment与.NET中的passing by reference、passing by value
Python文档中有一段话: Remember that arguments are passed by assignment in Python. Since assignment just cre ...
- call by value reference name python既不是按值传递也不是按引用传递 python复制原理 创建新对象 与 改变原对象
按名调用 Algol 按值调用 Java https://docs.python.org/3.6/faq/programming.html#how-do-i-write-a-function-with ...
- Python攻关之Django(一)
课程简介: Django流程介绍 Django url Django view Django models Django template Django form Django admin (后台数据 ...
- 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算
http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...
随机推荐
- lodash toString 转换为字符串
转换 value 为字符串. null 和 undefined 将返回空字符串.-0 将被转换为字符串"-0". _.toString(null); // => '' _.t ...
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性——位置设置偏向
LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...
- blog url.txt
java 回收机制与虚拟机http://www.cnblogs.com/zhanglei93/p/6636831.html Java 编程经验(牛人写的) 上/下http://www.thinksa ...
- IOS 多播委托(GCDMulticastDelegate)
原文:http://www.cnblogs.com/dagehaoshuang/p/4043264.html 在IOS中为了实现回调一般有如下几个方法: delegate 通知中心 block KVO ...
- JS 自动计算HTML的font-size
Rem尺寸解决方案,需要配合一些js动态设置<html>标签的font-size 和 viewport来配合 <script> (function(doc, win) { va ...
- C++程序设计(第4版)读书笔记_类型与声明
字符类型 #include <iostream> using namespace std; int main() { cout << << endl; cout & ...
- Graph Visualization
1. 什么是graph visualization? Graph visualization is a way of representing structural information as di ...
- unity mac 破解教程
1.安装好软件,我们解压破解包,有下面两个文件,Unity 和Unity_v5.x.ulf,放桌面. 2.打开终端. 3.输入文件地址,注意,我这上边的用户名,记得改成自己的用户名 ...
- Eclipse maven 项目红叉 编译不报错问题处理
项目右键-> Maven -> Update Maven Project 选中 :Force update 复选框
- html 标签 链接
<a href="http://www.baidu.com">百度</a> <a href="#here">here< ...