python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗
小结:
-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):
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; andletting 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 深复制 一切皆对象 尽量减少内存消耗的更多相关文章
- Python 变量作用域与函数
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- python变量作用域
[python变量作用域] 几个概念: python能够改变变量作用域的代码段是def.class.lamda. if/elif/else.try/except/finally.for/while 并 ...
- Python 变量作用域 LEGB (上)—— Local,Global,Builtin
Python 变量作用域的规则是 LEGB LEGB含义解释:L —— Local(function):函数内的名字空间E —— Enclosing function locals:外部嵌套函数的名字 ...
- Python 变量作用域 LEGB (下)—— Enclosing function locals
上篇:Python 变量作用域 LEGB (上)—— Local,Global,Builtin https://www.cnblogs.com/yvivid/p/python_LEGB_1.html ...
- Python 变量详解[学习 Python 必备基础知识][看此一篇就够了]
您的"关注"和"点赞",是信任,是认可,是支持,是动力...... 如意见相佐,可留言. 本人必将竭尽全力试图做到准确和全面,终其一生进行修改补充更新. 目录 ...
- Python变量作用域(一)
在一个程序中使用变量名时,Python创建.改变或者查找变量名都是在所谓的命名空间中进行的.作用域指的就是命名空间. Python中的变量名在第一次赋值时已经创建,并且必须经过赋值后才能够使用.由于变 ...
- python——变量作用域及嵌套作用域
----------------------------------------------------------------------------- 前言-------------------- ...
- python 变量作用域、闭包
先看一个问题: 下面代码输出的结果是0,换句话说,这个fucn2虽然已经用global声明了variable1,但还是没有改变变量的值 def func1(): variable1=0 def fun ...
- python变量作用域,函数与传参
一.元组传值: 一般情况下函数传递参数是1对1,这里x,y是2个参数,按道理要传2个参数,如果直接传递元祖,其实是传递一个参数 >>> def show( x, y ): ... p ...
随机推荐
- python Matplotlib 系列教程(三)——绘制直方图和条形图
在本章节我们将学习如何绘制条形图和直方图 条形图与直方图的区别:首先,条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少,矩形的高度表示每一组的 ...
- Java格式化CST时间(mysql date类型)
在从mysql导入数据时候,mysql里的日期是格林威治时间,普通格式化不行,这里总结一下格式化格林威治时间的方法: Date date = new Date(); System.out.printl ...
- [Luogu] P3846 [TJOI2007]可爱的质数
题目描述 给定一个质数P(2<=P<2312^{31}231),以及一个整数B(2<=B<P),一个整数N(2<=N<P). 现在要求你计算一个最小的L,满足BL≡ ...
- 笔试算法题(57):基于堆的优先级队列实现和性能分析(Priority Queue based on Heap)
议题:基于堆的优先级队列(最大堆实现) 分析: 堆有序(Heap-Ordered):每个节点的键值大于等于该节点的所有孩子节点中的键值(如果有的话),而堆数据结构的所有节点都按照完全有序二叉树 排.当 ...
- python 1-1模块介绍和使用
1. 什么是模块 1.1 模块就是一系列功能的集合体 1.1.1 模块有三种来源 1.内置的模块 2.第三方的模块 3.自定义模块 1.1.2 模块的格式: 1.使用Python编写的.py文件 2. ...
- buf.toString()
buf.toString([encoding[, start[, end]]]) encoding {String} 默认:'utf8' start {Number} 默认:0 end {Number ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- Gram-Schmidt向量正交化
正交:向量的内积为0,即相互垂直. 假如存在向量a,b确定一个平面空间,但是a,b向量并不垂直,如下图. 现在要在该平面内找出2个垂直的向量确定该平面: b和e垂直,接下来求解e: 根据向量计算法则: ...
- .DS_Store的说明
今天清理电脑时,突然发现好像有文件的地方都会出现一个.DS_Store文件,今天有时间,索性就查了一下,并做总结发表一篇吧,怕有什么影响,并未真正实施,仅仅供自己收藏,仅供大家参考. DS_ ...
- (dede)织梦系统二次开发笔记
(dede)织梦系统二次开发记录 --soulsjie 一.模板常用文件说明 模板文件都在文件夹templets下,我们以默认模板(default)为例,对模板文件结构进行分析: 首页模板文件目录 \ ...