推荐先看视频(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. linux 终端分屏命令vsp(转)

    比如:某文件夹下有文件:vector.cc, substr.cc 1.使用vim命令打开任意一个文件:vim vector.cc打开第一个文件.如下图所示: 2.按:"Esc"键 ...

  2. JAVA的IO编程:管道流

    掌握线程通讯流(管道流)的使用 管道流的主要作用是可以进行两个线程间的通讯,分为管道输入流(PipeOutputStream)和管道输出流(PipeInputStream). 如果要想进行管道输出,则 ...

  3. GBDT,随机森林

    author:yangjing ## time:2018-10-22 Gradient boosting decision tree 1.main diea The main idea behind ...

  4. 比isConnected()更靠谱的的获取socket实时连接状态!

    看到这个标题,预计非常多人会说用socket.isConnected()或者socket.isClosed()等方法来推断即可了,但其实这些方法都是訪问socket在内存驻留的状态,当socket和s ...

  5. JS 自动计算HTML的font-size

    Rem尺寸解决方案,需要配合一些js动态设置<html>标签的font-size 和 viewport来配合 <script> (function(doc, win) { va ...

  6. Mybatis学习-1(转自 csdn- http://my.csdn.net/hupanfeng 的文章)

    简介 MyBatis的前身叫iBatis,本是apache的一个开源项目, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBa ...

  7. Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理

    Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理   1. 事件冒泡1 2. 事件捕获1 3. 同时支持了事件捕获阶段和事件冒泡阶段ddEventListener的第三个参数1 ...

  8. java中ThreadLocalRandom类和Random类的使用

    package frank; import java.lang.*; import java.util.*;//工具类一般都在util里面 import java.util.concurrent.Th ...

  9. 字符串函数---strcmp()与strncmp()具体解释及实现

    一.strcmp()与strncmp() strcmp():strcmp(s1,s2);            比較两个字符串.        strncmp():strncmp(s1,s2);   ...

  10. ../lib//libscsdblog.so: undefined reference to `pthread_atfork'

    代码中遇到这个问题,但是在makefile中已经添加了-lpthread. 最后发现问题时,引入库的顺序,把-lpthread放在最后就可以了.