小结:

-1

+=修改旧变量地址的内容;=创建新增内存地址;

1、

id cpy 内存地址

id(object)

2、赋值语句的原理

不复制对象,而是建立目标和对象的绑定。

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

python 深入理解 赋值、引用、拷贝、作用域 - 江召伟 - 博客园 https://www.cnblogs.com/jiangzhaowei/p/5740913.html

a=[1,2,5]
b=a
print(id(a),'--',id(b)) b=[9,8]
print(id(a),'--',id(b)) f=7
g=f
print(id(f),'--',id(g))
g=888
print(id(f),'--',id(g)) class c:
def __init__(self,a):
self.a=a
def f(self):
print(self.a)
c1=c(66)
c2=c('str')
print(id(c1),'--',id(c2)) class cb:
def __init__(self):
self.a=a
def f(self):
print(123)
cb1=cb()
cb2=cb()
print(id(cb1),'--',id(cb2))
import copy gg=9

内存地址   内存标识 内存id

1794696 -- 1794696
1794696 -- 1794760
8791475577680 -- 8791475577680
8791475577680 -- 2024944
31727120 -- 31727176
31727288 -- 31727232

一切皆对象

尽量减少内存消耗

id 内存地址

id(object)

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

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):

  • shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

  • 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就可以赋值。



python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗的更多相关文章

  1. Python 变量作用域与函数

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  2. python变量作用域

    [python变量作用域] 几个概念: python能够改变变量作用域的代码段是def.class.lamda. if/elif/else.try/except/finally.for/while 并 ...

  3. Python 变量作用域 LEGB (上)—— Local,Global,Builtin

    Python 变量作用域的规则是 LEGB LEGB含义解释:L —— Local(function):函数内的名字空间E —— Enclosing function locals:外部嵌套函数的名字 ...

  4. Python 变量作用域 LEGB (下)—— Enclosing function locals

    上篇:Python 变量作用域 LEGB (上)—— Local,Global,Builtin https://www.cnblogs.com/yvivid/p/python_LEGB_1.html ...

  5. Python 变量详解[学习 Python 必备基础知识][看此一篇就够了]

    您的"关注"和"点赞",是信任,是认可,是支持,是动力...... 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 目录 ...

  6. Python变量作用域(一)

    在一个程序中使用变量名时,Python创建.改变或者查找变量名都是在所谓的命名空间中进行的.作用域指的就是命名空间. Python中的变量名在第一次赋值时已经创建,并且必须经过赋值后才能够使用.由于变 ...

  7. python——变量作用域及嵌套作用域

    ----------------------------------------------------------------------------- 前言-------------------- ...

  8. python 变量作用域、闭包

    先看一个问题: 下面代码输出的结果是0,换句话说,这个fucn2虽然已经用global声明了variable1,但还是没有改变变量的值 def func1(): variable1=0 def fun ...

  9. python变量作用域,函数与传参

    一.元组传值: 一般情况下函数传递参数是1对1,这里x,y是2个参数,按道理要传2个参数,如果直接传递元祖,其实是传递一个参数 >>> def show( x, y ): ... p ...

随机推荐

  1. Mybatis的一级二级缓存

    Mybatis提供了缓存机制,可以减轻数据库的压力,提高性能 Mybatis的缓存分为两级:一个是一级缓存,一个二级缓存 一级缓存:即默认使用的缓存SqlSession级别的缓存,只在sqlsessi ...

  2. 微信小程序 video组件----真机测试position:fixed无效 且有黑底

    1.问题描述 video组件fixed后,视频随页面滚动,且有个黑色底停留在页面. 页面滚动前 滚动后 这里贴一下修改前代码,在微信开发者工具看是没有任何问题的.在手机端测试就有以上的问题 <v ...

  3. JavaScript小技巧整理篇(非常全)

    能够为大家提供这些简短而实用的JavaScript技巧来提高大家编程能力,这对于我来说是件很开心的事.每天仅花上不到2分钟的时间中,你将可 以读遍JavaScript这门可怕的语言所呈现给我们的特性: ...

  4. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. poj 3253 Fence Repair (优先队列,哈弗曼)

    题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...

  6. Jquery 引擎模板 -template详解

    一.1.jTemplate简介 jTemplates是一个基于Jquery的js模板引擎插件.该引擎全部代码由JS实现,可以配合AJAX,JSON一起协同工作,模板内容可以用JS代码,实现了活动更新, ...

  7. 关于测试驱动的开发模式以及实战部分,建议看《Python Web开发测试驱动方法》这本书

    关于测试驱动的开发模式以及实战部分,建议看<Python Web开发测试驱动方法>这本书

  8. Flask组件:flask-sqlalchemy & flask-script & flask-migrate

    flask-sqlalchemy组件 项目目录结构: flask目录 # 项目名 |--- flaskdir |--- static # 静态文件 |--- templates # 模板 |--- m ...

  9. Linux下汇编语言学习笔记23 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  10. 使用JavaMail通过QQ/126服务器服务发送邮件

    https://blog.csdn.net/yidragon88xx/article/details/53230310