python学习之内存驻留机制简述

第四章

4.1 小数据池

4.1.1 代码块

一个模块,一个函数,一个类,甚至一个command命名都可以称之为一个代码块。

官方解释:

A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block. A script file (a file given as standard input to the interpreter or specified as a command line argument to the interpreter)

is a code block. A script command (a command specified on the interpreter command line with the ‘-c‘ option) is a code block. The string argument passed to the built-in functions eval() and exec() is a code block. A code block is executed in an execution frame. A frame contains some administrative information (used for debugging) and determines where and how execution continues after the code block’s execution has completed.

4.1.2 is与==的区别

​ == 判断左右两端的是否相等

​ is 判断的是两端内容的内存地址是否相同,如果返回的True,那么判定比较的两个变量使用的是同一个对象。

​ id的作用:用来查看开辟的空间所对应的地址

lst1 = [1,2,3]
lst2 = [1,2,3]
print(lst1 == lst2)  #True
print(lst1 is lst2)      #False

a = 'hahah'
b = 'hahah'
print(a == b)   #True
print(a is b)   #True

​ 注意:在python中,默认会把数字,字符串,布尔值进行缓存。

如果我们在从command控制台里写代码与在py文件中写代码机制是不一样的,因为环境不同所以缓存是不一样的,因为command控制台一行就是一个代码块。

总结:如果内存地址相同,那么值一定相同;如果值相等,内存地址不一定相等。

4.1.3 小数据池

原文:

在整数中:

​ The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined.

在字符串中:

​ Incomputer science, string interning is a method of storing only onecopy of each distinct string value, which must be immutable. Interning strings makes some stringprocessing tasks more time- or space-efficient at the cost of requiring moretime when the string is created or interned. The distinct values are stored ina string intern pool. –引自维基百科

​ ⼩数据池,一种数据缓存机制,也被称为驻留机制。为提高效率,固定数据类型使用同一个内存地址;各⼤编程语⾔中都有类似的东⻄,在一定范围内来说,常量池,小数据池指的都是同⼀个内容。

​ ⼩数据池只针对: 整数,字符串,布尔值。其他的数据类型不存在驻留机制。

在控制台中:

​ 1.数字:在-5~256之间的数值可以被缓存;

​ 2.布尔值:

​ 3.字符串:

​ 字符串的长度为0或1时,会默认进行缓存;

​ 字符串自己定义,长度不限,但字符串只包含字母,数字,下划线的时会缓存;

​ 用乘法得到的字符串:·乘数为1,仅包含字母,数字,下划线时会缓存,如果包含其他字符, ⽽⻓度<=1 也会被驻存; ·乘数⼤于1,仅包含数字, 字母, 下划线这个时候会被缓存,但字符串长度不能大于20;

​ 特殊字符(中文除外), 定义为 1 个字符时,会进行驻留;

在代码块内部(一个py文件):

​ 1.数字:基本上都可以进行缓存,但是临界值没有明确的限定;运算结果-5~256之间会缓存

​ 2.布尔值:可以进行缓存

​ 3.字符串:默认字符串都缓存,如果有乘法运算与控制台相同;python3.7版本乘法不能超过4096

总结:执行同⼀个代码块时, 遇到初始化对象的命令时,他会将初始化的这个变量与值存储在一个字典中, 在遇到新的变量时, 会先在字典中查询记录, 如果有同样的记录那么它会重复使⽤这个字典中的之前的这个值。

如果是不同的代码块, 他就会看这个两个变量是否是满足小数据池的数据, 如果是满⾜小数据池的数据则会指向同⼀个地址。 所以: a, b的赋值语句分别被当作两个代码块执⾏,但是他们不满足⼩数据池的数据所以会得到两个不同的对象,因⽽is判断返回False。

优点: 能够提⾼一些字符串,整数的处理理速度,省略的创建对象的过程;

缺点: 在'池'中创建或者插入新的内容会花费更多的时间。

强行驻留

from sys import intern ##自己进行驻留

指定驻留:通过sys模块中的intern()函数来指定驻留内容

from sys import intern
a = intern('hahah'*5)
b = intern('hahah'*5)
print(a == b)  #True
print(a is b)  #True

4.1.4 编码的补充

  1. python2中默认使⽤的是ASCII码,所以不支持中⽂;如果需要在Python2中更改编码,需要在⽂件的开始编写:
-*- encoding:utf-8 -*-
  1. python3中: 内存中使⽤的是unicode码,因为Unicode是定长的;代码使用utf-8来存储

编码回顾:

  1. ASCII : 最早的编码,⾥面有英⽂大写字母, ⼩写字母, 数字, 一些特殊字符,没有中⽂,8个01代码, 8个bit, 1个byte;
  2. GBK: 中⽂国标码, 里⾯包含了ASCII编码和中文常⽤编码,16个bit, 2个byte
  3. UNICODE: 万国码, ⾥面包含了全世界所有国家文字的编码, 32个bit, 4个byte, 包含了ASCII
  4. UTF-8: 可变⻓度的万国码,是unicode的⼀种实现,最⼩字符占8位

    1.英⽂: 8bit --1byte

    2.欧洲文字:16bit --2byte

    3.中文:24bit --3byte

    综上, 除了ASCII码以外, 其他信息不能直接转换

编码encode

.encode('UTF-8')--结果是Bytes类型,但依然是原来的字符串,其表现形式:

  1. 英文 b'alex' 英⽂的表现形式和字符串相同
  2. 中⽂ b'\xe4\xb8\xad' 这是⼀个汉字的UTF-8的bytes表现形式

​ 英文编码之后的结果和源字符串⼀致;中⽂编码之后的结果根据编码的不同,编码结果也不同。 由此可见, ⼀个中⽂的UTF-8编码是3个字节,一个GBK的中⽂编码是2个字节,编码之后的类型就是bytes类型。在⽹络传输和存储的时候我们python是保存和存储的bytes类型。那么在对方接收的时候,也是接收的bytes类型的数据。

s = b'abc'  #如果元素是子母等,不需要encode
s1 = '哈哈'
print(s)
print(s1.encode('gbk'))

解码decode

用什么编码,就用什么解码

.decode('utf-8')

编码和解码的时候都需要制定编码集格式。

utf-8与gbk不能直接相互转换。