推荐先看视频(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的更多相关文章

  1. Python 字典 values() 方法

    描述 Python 字典 values() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回字典中的所有值. 语法 values() 方法语法: D.values() 参数 无 ...

  2. 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 ...

  3. pytest框架执行自动化测试时使用pycharm正常运行,使用cmd或Terminal报错:Hint: make sure your test modules/packages have valid Python names.

    问题描述: 使用pytest框架做接口自动化测试时,在测试用例所在的.py文件下使用pycharm的run功能可以正常跑用例,使用cmd运行窗口或Terminal则报下图中的错误: Hint: mak ...

  4. 短篇文档兼职看过来 python 课后作业 assignment project

    文档兼职 开题报告 读后感 课后作业 等 代写 编程,Java ,Python,R,等语言的,国内外课程作业指导,写作. 有经验,有作品,成交快,放心! 可联系 QQ 550987425

  5. python two-dimensional array assignment initialize

    #if you want to initialize a 9*9 two-dimensional array [([""]*9) for i in range(9)] #cauti ...

  6. 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 ...

  7. 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 ...

  8. Python攻关之Django(一)

    课程简介: Django流程介绍 Django url Django view Django models Django template Django form Django admin (后台数据 ...

  9. 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算

    http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...

随机推荐

  1. VIM-不常用或不知道的技巧

    cc 清除一行 并在本行编辑, 同理 cw :32,65d 多行删除 g/pattern/d 删除包含特定字符的行 v/pattern/d 删除不包含指定字符的行 等同于 g!/pattern/d y ...

  2. js实现可兼容IE、FF、Chrome、Opera及Safari的音乐播放器

    代码如下: /** 音乐播放器 * @param obj 播放器id * @param file 音频文件 mp3: ogg: * @param loop 是否循环 */ function audio ...

  3. [DevExpress]XtraTabControl右键加入关闭当前页、关闭其它页、所有关闭的实现

    private void xtraTabControl_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButton ...

  4. unity, 由于project settings中time scale变成0导致动画不播放

    在Mac和iOS间多次switch platform之后,忽然发现开始scene的动画不播了.进入游戏后再切回来动画恢复正常. 检查了scene的逻辑,发现没有任何问题.删除了Temp和Library ...

  5. Atitit.upnp SSDP 查找nas的原理与实现java php c#.net c++

    Atitit.upnp SSDP 查找nas的原理与实现java php c#.net c++ 1. 查找nas的原理1 2. 与dlna的关系1 3. 与ssdp的关系1 4. Cling - Ja ...

  6. Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle

    Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle 1. 主键1 2. uniq  index2 3.  ...

  7. dubbo_分布式框架dubbox介绍

    1.Dubbo概述Dubbo是阿里巴巴开源出来的一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及作为SOA服务治理的方案.简单的说,dubbo就是个服务框架,如果没有分布式的 ...

  8. 如何在Linux下统计高速网络中的流量

    参考: http://www.geekfan.net/5558/ http://blog.jobbole.com/23638/ http://www.csdn.net/article/2014-03- ...

  9. jQuery EasyUI DataGrid在MVC中的运用-基本属性并实现分页

    ※ datagrid的基本属性和方法  ※ datagrid分页在前后台的实现 最终效果:    与视图显示对应的view model   public class Book public strin ...

  10. hadoop工作相关

    网站点击流日志分析,客户画像,推荐系统,bi系统