CPython implementation detail: This is the address of the object in memory.

copy — Shallow and deep copy operations — Python 3.8.2rc1 documentation https://docs.python.org/3.8/library/copy.html

Assignment statements in Python do not copy objects, they create bindings between a target and an object.

a = b = c = 1

e, d, f = 1, 1, 1

a = 4

a1 = b1 = c1 = '1'

e1, d1, f1 = '1', '1', '1'

a1 = '2'

import copy

x = copy.deepcopy(a)

z = copy.copy(a)

y = a

a = 5

x1 = copy.deepcopy(e1)

z1 = copy.copy(e1)

y1 = e1

e1 = '2'

lc = [1, [2, 3]]

lc1 = copy.copy(lc)

lc2 = copy.deepcopy(lc)

lc = [1, [2, 3, 4]]

dc = {'a':12,12:'a'}

dc1 = copy.copy(dc)

dc2 = copy.deepcopy(dc)

dc = {'a':123,12:'ab'}

dcr = {'a':12,12:'a'}

dcr0=dcr

dcr1 = copy.copy(dcr)

dcr2 = copy.deepcopy(dcr)

dcr = {'a':123,12:{1:1,'a':'a'}}

dcrr = {'a':123,12:{1:1,'a':'a'}}

dcrr0=dcrr

dcrr1 = copy.copy(dcrr)

dcrr2 = copy.deepcopy(dcrr)

dcrr = {'a':123,12:{11:1,'aa':'aa'}}

t = 7

上述赋值语句 右边值的改变均没有引起左边值的改变。

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.

Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.

Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.

The deepcopy() function avoids these problems by:

keeping a memo dictionary of objects already copied during the current copying pass; and

letting user-defined classes override the copying operation or the set of components copied.

关于Python中的引用 - 东去春来 - 博客园 https://www.cnblogs.com/yuyan/archive/2012/04/21/2461673.html

x,y=1,2

x,y=y,x+y

生成器 - 廖雪峰的官方网站 https://www.liaoxuefeng.com/wiki/1016959663602400/1017318207388128

注意,赋值语句:

a, b = b, a + b

相当于:

t = (b, a + b) # t是一个tuple

a = t[0]

b = t[1]

但不必显式写出临时变量t就可以赋值。

The deepcopy() function avoids these problems by:

keeping a memo dictionary of objects already copied during the current copying pass; and

letting user-defined classes override the copying operation or the set of components copied.

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

Shallow copies of dictionaries can be made using dict.copy(), and of lists by assigning a slice of the entire list, for example, copied_list = original_list[:].

Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.

In order for a class to define its own copy implementation, it can define special methods __copy__() and __deepcopy__(). The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the __deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy()function with the component as first argument and the memo dictionary as second argument.

See also

Module pickleDiscussion of the special methods used to support object state retrieval and restoration.

python sizeof_python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗 赋值语句的原理...相关推荐

  1. Python中变量作用域问题

    我们经常听说Python函数访问局部变量.全局变量:在定义装饰器的时候,还会使用自由变量.这些不同的变量是如何赋值.初始化.查找及修改的呢?各自的作用细则又是什么样的呢?本篇尝试解答这个问题. Pyt ...

  2. python中变量作用域

    python中变量作用域采取以下规则: 1.python能够改变变量作用域的代码段是def.class.lamda. 2.if/elif/else.try/except/finally.for/whi ...

  3. python legb_Python变量作用域LEGB用法解析

    这篇文章主要介绍了Python变量作用域LEGB用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 闭包就是, 函数内部嵌套函数. 而 装饰器只 ...

  4. Python 的变量作用域和 LEGB 原则

    在 Python 程序中创建.改变或查找变量名时,都是在一个保存变量名的地方进行中,那个地方我们称之为命名空间.作用域这个术语也称之为命名空间. 具体地说,在代码中变量名被赋值(Python 中变量声 ...

  5. python函数变量的作用域_学不会的Python函数——变量作用域

    1. LEGB函数 Python中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.我们先来看一段代码. 上述代码有两个变量a,当在test函数中输出变量a的值是,为什么 ...

  6. Python基础-变量作用域

    1.函数作用域介绍 函数作用域 Python中函数作用域分为4种情况: L:local,局部作用域,即函数中定义的变量: E:enclosing,嵌套的父级函数的局部作用域,即包含此函数的上级函数的局 ...

  7. Python之变量作用域

    文章目录 一 变量作用域 1. Local(局部变量) 2. Enclosed(嵌套) 3. Global(全局) 4. Built-in(内置) 二 变量使用规则 三 变量的修改 1. global ...

  8. python的变量作用域

    1. 不在函数体内的变量或者在 if __name__=='__main__'中的变量,都是全局变量,注意访问这些全局变量的速度是比较慢的,因为这些全局变量放在一个全局的表中,需要查找 2. 在函数体 ...

  9. python代码变量作业_1作业python数据类型 条件循环 列表

    变量 python中不用像C++一样先定义数据类型再赋值,可以直接赋字符串类型.字典类型.元组类型.列表类型: python的变量名只能包含数字 字母 下划线,不能以python的关键字命名,可以以下 ...

最新文章

  1. 万字长文!线性代数的本质课程笔记完整合集
  2. 下列代码之后的结果为()?
  3. JDBC批量操作批量增加批量修改
  4. php 巧用逻辑运算符,php的神奇逻辑运算符
  5. Flutter for Web 详细预研
  6. 快速更新android sd卡,Android 动态加载sd卡的jar文件实现更新jar方法
  7. 他是BAT 100万+年薪大数据专家,今天你可以免费学习他的内部课程,仅限100人...
  8. Leetcode 863.二叉树中所有距离为K的结点
  9. 判断进程是否正在运行
  10. 2012-1-31学习日记
  11. CDN技术详解(电子书)下载链接
  12. 假设检验(Hypothesis Testing)的内涵及步骤
  13. 2022年全球市场GPS追踪装置总体规模、主要生产商、主要地区、产品和应用细分研究报告
  14. 手机影像ISP流程:AWB(1)
  15. [电脑驱动向]笔记本键盘失灵,电脑插耳机没反应,不要着急拿去物理维修,可能是bios驱动需要更新
  16. Oracle数据库表空间整理回收与释放操作
  17. MLNLP顶会论文发表总榜:谷歌最狂,清北入前十,周明、张岳、刘挺华人前三
  18. 实验6:安装EVE-NG
  19. ps 2022 保存打开文件闪退解决方法
  20. win11剪贴板数据如何删除 Windows清空剪贴板数据的步骤方法

热门文章

  1. 前沿技术分享,让你在算法圈“技”高一筹
  2. 【NLP保姆级教程】手把手带你CNN文本分类(附代码)
  3. 干货!Facebook多账号养号技巧,对封号说拜拜!
  4. 如何安装mysql5.7.15_ubuntu16.04安装mysql5.7.15
  5. 从 重复叠加字符串匹配 看Java String源码中的contains方法
  6. Leetcode每日一题:559.maximum-depth-of-n-ary-tree(N叉树的最大深度)
  7. Java手写线程池(不带返回值、带返回值)
  8. Linux电驴客户端,ubuntu装电驴
  9. 【第8篇】Python爬虫实战-批量删除csdn私信记录
  10. python datetime和字符串如何相互转化?